Hello, guys welcome to my new article on introduction to Java programming language in which I will tell you what java programming is and how it works.
In this article, I will explain what java programming is, its operators, data types, and how to run java programs in Windows and Linux.
Java Programming
Java is a programming language used for developing and running applications. It was first released by Sun Microsystems in 1995 and is now a subsidiary of Oracle Corporation. It is an object-oriented language, which means that it is based on the concept of “objects”, which can contain data and code that manipulates that data. Java is known for being fast, reliable, and secure. It is also platform-independent. Platform-independent means Java programs can run on any device that has a Java Virtual Machine (JVM) installed.
Java code should be saved with the (.java) extension to be executed.
Syntax: –
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Uses of Java Programming
Various uses of java programming are listed below: –
- Developing desktop applications
- Developing web applications
- Developing mobile applications
- Developing games
- Internet of Things (IoT) development
- Artificial Intelligence and Machine Learning
- Embedded systems development
- Developing smart cards and other secure devices
- Developing blockchain applications
- Developing chatbots and virtual assistants
Operators in Java Programming
In java programming language operators are divided into 3 categories which are Unary, Binary, and Ternary. There are a total of 10 types of operators present in java, which are given below: –
- Arithmetic operator
- Relational operator
- Logical operator
- Boolean logical operator
- Increment/Decrement operator
- Bitwise operator
- Compound operator
- Assignment operator
- Instance operator
- Conditional operator
Arithmetic Operator
Arithmetic operators are used for performing mathematical operations like addition, subtraction, multiplication, and division. The basic arithmetic operators are +, -, *, and /. There is one more arithmetic operator which is % (modulus). It is a binary operator in which two operands are required to perform any operation.
Ex: –
public class Main {
public static void main(String args[]) {
int x = 2;
int y = 4;
int z = x + y;
System.out.println(z);
}
}
Relational Operator
A relational operator is used to compare two values and determine the relationship between them. The relational operators available in Java are <, >, <=, >=, ==, !=. Relational operators are binary operators in which two operands are required to perform any operation.
Ex: –
public class Main {
public static void main(Strin args[]) {
int x = 2;
int y = 4;
System.out.println(x > y);
}
}
Logical Operator
A logical operator is used to combine multiple relational expressions and create a more complex Boolean expression. The logical operators available in Java are &&(and), ||(or), !(not). It is a binary operator in which two operand is required to perform any operation.
Ex: –
public class Main {
public static void main(String args[]) {
int x = 5;
int y = 10;
int z = 15;
if (x < y && y < z) {
System.out.println("x is less than y and y is less than z");
}
}
}
Boolean logical Operator
The Boolean logical operators are used to perform logical operations on Boolean variables or expressions. The Boolean logical operators in Java are &, |, !, ^. It is a binary operator in which two operans are required to perform any operation.
Ex: –
public class Main {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
if (a | b) {
System.out.println("At least one of the expressions is true");
}
}
}
Increment/Decrement Operator
The increment and decrement operators are used to increase or decrease the value of a variable by a certain amount. The increment operator ++ and the decrement operator — can be used as prefixes or postfix operators. These are unary operators in which only one operand is required to perform any operation.
public class Main {
public static void main(String args[]) {
int a = 5;
int b = ++a;
System.out.println(b);
}
}
Bitwise Operator
Bitwise operators are used for performing operations on the individual bits of an integer value. The bitwise operators in Java are &
(and),|
(or), ^
(xor), ~
(not), <<
(left shift), >>
(right shift), and >>>
(unsigned right shift). It is a binary operators.
Ex: –
public class Main {
public static void main(String args[]) {
int a = 5;
int b = 3;
int c = a ^ b;
System.out.println(c);
}
}
Compound Operator
Compound operators are used for combining an arithmetic or a bitwise operator with an assignment operator. It allows you to perform an operation and assign the result to a variable in a single step. It is a binary operator. The most common compound operators in Java are given below: –
+=
(add and assign)-=
(subtract and assign)*=
(multiply and assign)/=
(divide and assign)%=
(modulus and assign)&=
(bitwise and assign)|=
(bitwise or and assign)^=
(bitwise xor and assign)<<=
(left shift and assign)>>=
(right shift and assign)>>>=
(unsigned right shift and assign)
Ex: –
public class Main {
public static void main(String args[]) {
int a = 5;
a += 2;
System.out.println(a);
}
}
Assignment Operator
The assignment operator =
is used to assign a value to a variable. The value on the right side of the operator is stored in the variable on the left side of the operator. It is a unary operator.
Ex: –
public class Main {
public static void main(String args[]) {
int a = 10;
int b = a;
System.out.println(b);
}
}
Instance Operator
An instance of an operator is used to determine whether an object is an instance of a particular class or interface. The instance of the operator returns a boolean value of true if the object is an instance of the specified class or interface, and false if it is not. It is a unary operator.
Ex: –
public class Main {
public static void main(String args[]) {
String str = "Hello";
if (str instanceof String) {
System.out.println("str is an instance of String");
}
}
}
Conditional Operator
The conditional operator is a shorthand way of writing an if-else statement. Conditional operators are (?) and (:). It is a ternary operator in which three operands are required for performing an operation. The syntax of the conditional operator is given below: –
expression1 ? expression2 : expression3
Ex: –
public class Main {
public static void main(String args[]) {
int a = 10;
int b = 20;
int c = (a > b) ? a : b;
System.out.println(c);
}
}
Data types in Java Programming
Data types are the concept which is used to show or describe the nature of the variable whether it is an integer or character or another. In Java Data types are divided into four categories which are: –
- Integer
- Decimal
- Character
- Boolean
Data Type | Size in byte | Size in bits | Description |
byte | 1 byte | 8 bits | Stores whole numbers from -128 to 127 (Integer) |
short | 2 bytes | 16 bits | Stores whole numbers from -32,768 to 32,767 (Integer) |
int | 4 bytes | 32 bits | Stores whole numbers from -2,147,483,648 to 2,147,483,647 (Integer) |
long | 8 bytes | 64 bits | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (Integer) |
float | 4 bytes | 32 bits | Stores fractional values up to 6 to 7 digits (Decimal) |
double | 8 bytes | 64 bits | Stores fractional values up to 15 digits (Decimal) |
char | 0.125 byte | 1 bit | Stores a single letter/alphabet or ASCII value |
boolean | 2 bytes | 16 bits | Stores true or false values |
If you have any queries regarding the above content, or you want to update anything in the content, then contact us with your queries. You can directly post your question in the group.
Connect with us on these platforms