Java

Basics of Java Programming

Overall millions of users prefer Java Programming language as the language of coding. Basically, Java is a pure object-oriented, reliable, robust, and secure language. Further, it is a general-purpose high-level language. In order to learn any programming language, it is very important to practice writing programs. Therefore, some basic code examples are given here.

Examples of Java Programming

The following section contains several basic examples of Java programming.

Display a Message

To begin with, let us write a simple program in Java that displays a message.

package welcome;
public class Welcome {

	public static void main(String[] args) {
		System.out.println("Welcome to Java Programming with programmingempire.com");
	}
}

Output

Display a Message using Java Programming Language
Display a Message using Java Programming Language

Variable Declaration and Initialization

As shown in the following code, we can initialize variables with constant values as well as an expression containing previously assigned variables.

package welcome;
public class Welcome {

	public static void main(String[] args) {
		int a, b, c;
		a=10; b=20; c=a+b;
		System.out.println("c = "+c);
	}
}

Output

c = 30

Another example of variable initialization is shown below. In addition to the variables a, and b, the variable c is assigned using the values of a, and b.

package welcome;
public class Welcome {

	public static void main(String[] args) {
		int a=12, b=23, c=a*a+b*b;
		System.out.println("c = "+c);
	}
}

Output

c = 673

In fact, we can also use a library function to provide initial value to a variable. As shown below, the variable c is initialized using the value of a, and b as well as by using the sqrt() function of Math class.

package welcome;
public class Welcome {
	public static void main(String[] args) {
		int a=12, b=23;
		double c=Math.sqrt(a*a+b*b);
		System.out.println("c = "+Math.round(c));
	}
}

Output

c = 26

Type Casting

In order to assign a value of one type to a variable of a different type, we perform type casting. Basically, we can have two kinds of type casting – implicit type casting and explicit type casting. Whenever, we assign a value of a data type having smaller range to the variable of a data type with larger range, implicit type casting occurs. In contrast, if a value of data type with larger rage is assigned to a variable of data type with smaller range, an explicit type casting is required. Further, the implicit type casting doesn’t require any additional keyword or modification in the expression. While, in case of explicit type casting, the destination data type in parantheses should appear before the value or expression on the right hand side of the assignment statement. As an illustration see the examples given below.

Example of Implicit Type Casting

The following code shows an example of implicit type casting. As can be seen, b1 , and b2 are two variables of byte type. However, the expression b1*b2 is of type int. Since we are type casting an int expression to a double type variable, there is no need of type casting operator.

package welcome;
public class Welcome {
	public static void main(String[] args) {
		byte b1=100;
		byte b2=120;
		double c=b1*b2;
		System.out.println("c = "+c);
	}
}

Output

c = 12000.0

Example of Explicit Type Casting

The following code shows an example of explicit type casting. Since, the multiplication operator results in an int type value, we need to assign it in an int type variable. Or it may be a variable of another data type of higher range. However, the expression is being assigned to a byte type variable. Therefore, it needs a casting operator. Further, the resulting value at right hand side is larger than the maximum value that a byte type variable can hold. Therefore, we get a truncated value as shown in the output.

package welcome;
public class Welcome {
	public static void main(String[] args) {
		byte b1=100;
		byte b2=120;
		byte c=(byte)(b1*b2);
		System.out.println("c = "+c);
	}
}

Output

c = -32


Further Reading

Evolution of JavaScript from ES1 to ES2020

Understanding Document Object Model (DOM) in JavaScript

Introduction to Programming in TypeScript

Creating Classes in TypeScript

Working with Arrays in TypeScript

Significance of Tuples in TypeScript

Explaining Interfaces in TypeScript with Examples

How to Create and Use Arrow Functions in TypeScript

Understanding JSON Web Tokens

Java Practice Exercise

Introduction to HTML DOM Methods in JavaScript

Changing Style of HTML Elements using getElementsByClassName() Method

Show or Hide TextBox when Selection Changed in JavaScript

JavaScript Code Examples

Callback Functions in JavaScript

JavaScript Code for Event Handling

What is Asynchronous JavaScript?

Understanding HTTP Requests and Responses

Leave a Reply

Your email address will not be published. Required fields are marked *