Java is a popular object-oriented programming language, known for its
stability, scalability, and security. In this chapter, we learned about the basics
and features of Java.
Hello World Program
Let’s start with the classic “Hello World” program. This is the first program
that many people write when they are learning a new programming
language:
Public class HelloWorld {
Public static void main(String[] args) {
System.out.println(“Hello, World!”);
}
}
In this example, we define a class called HelloWorld, which contains a
method called main. The main method is the entry point of the program.
When we run this program, it will print the string “Hello, World!” to the
console.
Variables and Data Types
In Java, we can define variables to store data. Here’s an example of how to
define and initialize a variable:
Int number = 42;
In this example, we define a variable called number with the data type int.
We initialize the variable to the value 42. Java supports several data types,
including:
Byte: a signed 8-bit integer (-128 to 127)
Short: a signed 16-bit integer (-32,768 to 32,767)
Int: a signed 32-bit integer (-2,147,483,648 to 2,147,483,647)
Long: a signed 64-bit integer
Float: a single-precision floating-point number
, Double: a double-precision floating-point number
Boolean: a boolean value (true or false)
Char: a Unicode character
Operators
Java supports several operators, including:
Arithmetic operators (+, -, *, /, %)
Assignment operators (=, +=, -=, *=, /=, %=)
Comparison operators (==, !=, <, >, <=, >=)
Logical operators (&&, ||, !)
Bitwise operators (&, |, ^, ~, <<, >>, >>>)
Here’s an example of using arithmetic operators:
Int a = 10;
Int b = 5;
Int result = a + b; // 15
Result = a – b; // 5
Result = a * b; // 50
Result = a / b; // 2
Result = a % b; // 0
Control Structures
Java supports several control structures, including:
If statement
If-else statement
If-else if statement
Switch statement
Loops (for, while, do-while)
Break and continue statements
stability, scalability, and security. In this chapter, we learned about the basics
and features of Java.
Hello World Program
Let’s start with the classic “Hello World” program. This is the first program
that many people write when they are learning a new programming
language:
Public class HelloWorld {
Public static void main(String[] args) {
System.out.println(“Hello, World!”);
}
}
In this example, we define a class called HelloWorld, which contains a
method called main. The main method is the entry point of the program.
When we run this program, it will print the string “Hello, World!” to the
console.
Variables and Data Types
In Java, we can define variables to store data. Here’s an example of how to
define and initialize a variable:
Int number = 42;
In this example, we define a variable called number with the data type int.
We initialize the variable to the value 42. Java supports several data types,
including:
Byte: a signed 8-bit integer (-128 to 127)
Short: a signed 16-bit integer (-32,768 to 32,767)
Int: a signed 32-bit integer (-2,147,483,648 to 2,147,483,647)
Long: a signed 64-bit integer
Float: a single-precision floating-point number
, Double: a double-precision floating-point number
Boolean: a boolean value (true or false)
Char: a Unicode character
Operators
Java supports several operators, including:
Arithmetic operators (+, -, *, /, %)
Assignment operators (=, +=, -=, *=, /=, %=)
Comparison operators (==, !=, <, >, <=, >=)
Logical operators (&&, ||, !)
Bitwise operators (&, |, ^, ~, <<, >>, >>>)
Here’s an example of using arithmetic operators:
Int a = 10;
Int b = 5;
Int result = a + b; // 15
Result = a – b; // 5
Result = a * b; // 50
Result = a / b; // 2
Result = a % b; // 0
Control Structures
Java supports several control structures, including:
If statement
If-else statement
If-else if statement
Switch statement
Loops (for, while, do-while)
Break and continue statements