Java is a popular and widely-used programming language that is well-suited for developing a variety of applications, including desktop applications, mobile applications, and web applications. This article from Novatesting will provide an overview of some of the basic concepts of Java, including variables, data types, operators, control structures, and arrays.
- Variables in Java

Variables in Java are used to store values that can be used later in the program. Variables must be declared with a data type and an optional value. There are several different types of variables in Java, including local variables, instance variables, and static variables.
Declaring variables in Java:
int age;
String name;
double salary;
– Local variables in Java: Local variables are variables that are declared within a method or block of code. They are only accessible within the scope in which they are declared.
– Instance variable (global variable) in Java: Instance variables, also known as global variables, are variables that are declared within a class but outside of a method. They are accessible from any method within the class.
– Static variables in Java: Static variables are variables that are shared among all instances of a class. They are declared with the “static” keyword.
- Data types in Java
In Java, data types are used to specify the type of value that a variable can store. There are two main types of data types in Java: primitive data types and object data types.
Primitive data types: Primitive data types are basic data types that are built into the Java language. They include int, double, char, boolean, and others.
byte
: This data type is used to store small numerical values and can hold a value between -128 to 127.
byte age = 32;
short
: This data type is used to store numerical values that are larger than byte
and can hold a value between -32768 to 32767.
Example:
short year = 2022;
int
: This data type is used to store integers and can hold a value between -2147483648 to 2147483647. It is the most commonly used data type for representing numbers.
Example:int population = 1000000;
long
: This data type is used to store very large integers and can hold a value between -9223372036854775808 to 9223372036854775807.
Example:
long distance = 1000000000;
float
: This data type is used to store floating-point numbers with single precision.
Example:
float height = 5.6f;
double
: This data type is used to store floating-point numbers with double precision.
Example:
double salary = 5000.50;
char
: This data type is used to store characters, including letters, numbers, and symbols.
Example:
char initial = ‘A’;
boolean
: This data type is used to store logical values, either true or false.
Example:
boolean isValid = true;
Object data types: Object data types are classes that can be used to create objects. They include String, ArrayList, HashMap, and others.
import java.util.Date;
public class ObjectDataTypesExample {
public static void main(String[] args) {
// Date object
Date currentDate = new Date();
System.out.println(“Current date: ” + currentDate);
// String object
String name = "John Doe";
System.out.println("Name: " + name);
}
}
- Operators in Java
Operators are symbols that perform operations on values. Java provides several types of operators, including arithmetic operators, bit operators, relational operators, logical operators, conditional operators, and assignment operators.
- Arithmetic Operators: These operators perform basic arithmetic operations, such as addition, subtraction, multiplication, division, and remainder. For example:
+
,-
,*
,/
, and%
. - Bitwise Operators: These operators perform bit-level operations on operands. For example:
&
,|
,^
,~
,<<
, and>>
. - Relational Operators: These operators compare two operands and return a boolean result indicating if the comparison is true or false. For example:
<
,>
,<=
,>=
,==
, and!=
. - Logical Operators: These operators perform logical operations on operands and return a boolean result indicating if the operation is true or false. For example:
&&
,||
, and!
. - Conditional Operator: The conditional operator
? :
is a ternary operator that takes three operands and returns one of the two values based on the value of the first operand. - Assignment Operators: These operators are used to assign values to variables. For example:
=
,+=
,-=
,*=
,/=
, and%=
.
In Java, the precedence of operators determines the order in which operations are performed. The operators with higher precedence are evaluated before the operators with lower precedence. For example, the arithmetic operator *
has a higher precedence than the operator +
, so the expression 2 + 3 * 4
will be evaluated as 2 + (3 * 4)
first, and then the result will be added to 2
.
You can change the order of precedence by using parentheses. For example, the expression (2 + 3) * 4
will be evaluated as (2 + 3)
first, and then the result will be multiplied by 4
.
- if-else clause in Java
The if-else clause is a control structure that is used to execute code conditionally. The syntax for an if-else clause is as follows:
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
- for loop in Java
The for loop is a control structure that is used to repeat a block of code a specified number of times. The syntax for a for loop is as follows:
for (initialization; condition; increment/decrement) {
// code to be executed
}
- Array in Java
An array is a data structure that is used to store a collection of values of the same data type. Arrays can be declared and initialized in Java as follows:
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {“John”, “Jane”, “Jim”};
Arrays can be accessed using an index, which starts at 0 for the first element and increments for each subsequent element. The length of an array can be found using the “length” property.
Arrays can be useful for storing and processing large amounts of data, and they can be used in conjunction with loops and other control structures to perform complex operations.
These are just some of the basic concepts of Java programming that are essential for developing applications. By understanding these concepts, you will be well on your way to becoming a skilled Java developer. Remember to keep practicing and experimenting with different code examples to reinforce your understanding of these concepts.