Hello everyone I am RAUSHAN RANJAN
In this post you are going to get all the neccessary that you need to learn in java. You don't need to search other places to get the exact detail.
I know many of you are new in programming. Don't worry, you will get every minor details that is required to understand java from Basics.
Let's jump into the topic
1. DATA TYPES:
Data types specify the type of data that a variable can store. In Java, there are two main categories of data types:
a. Primitive Data Types:
- byte: represents a 1-byte integer (-128 to 127).
- short: represents a 2-byte integer (-32,768 to 32,767).
- int: represents a 4-byte integer (-2,147,483,648 to 2,147,483,647).
- long: represents an 8-byte integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).
- float: represents a 4-byte floating-point number with decimal precision.
- double: represents an 8-byte floating-point number with higher decimal precision.
- boolean: represents a boolean value (true or false).
- char: represents a single character.
b. Reference Data Types:
- Arrays: represents a collection of elements of the same type.
- String: represents a sequence of characters.
Example:
int age =
25;
double
salary = 50000.50;
boolean
isStudent = true;
String name
= "John Doe";
int[]
numbers = {1, 2, 3, 4, 5};
2. VARIABLES:
Variables
are used to store data in a program. In Java, variables must be declared with a
specific data type before they can be used.
Example:
int num1;
// Declaration
num1 = 10; // Assignment
int num2 =
20; // Declaration and assignment in one line
3. OPERATORS:
Operators are used to perform operations on variables and values. Java supports various types of operators, including:
- Arithmetic Operators: +, -, *, /, % (remainder)
- Assignment Operators: =, +=, -=, *=, /=, %=
- Comparison Operators: ==, !=, >, <, >=, <=
- Logical Operators: && (AND), || (OR), ! (NOT)
- Increment/Decrement Operators: ++, --
- Bitwise Operators: &, |, ^ (XOR), ~ (NOT), << (left shift), >> (right shift)
Example:
int a = 10,
b = 5;
int sum = a
+ b;
int product
= a * b;
boolean
result = (a > b) && (a != b);
4. CONDITIONAL STATEMENTS (if-else):
Conditional statements allow the execution of different blocks of code based on certain conditions. The most common conditional statement is the if-else statement.
Example:
int age = 18;
if (age
>= 18) {
System.out.println("You are eligible
to vote.");
} else {
System.out.println("You are not
eligible to vote.");
}
5. LOOPS:
Loops are used to repeat a block of code multiple times. Java provides three main types of loops:
a. for
Loop:
for (int i
= 1; i <= 5; i++) {
System.out.println(i);
}
b. while
Loop:
int i = 1;
while (i
<= 5) {
System.out.println(i);
i++;
}
c. do-while Loop:
int i = 1;
do {
System.out.println(i);
i++;
} while (i
<= 5);
6. SWITCH-CASE:
The switch case statement is used to perform different actions based on different values of a variable or an expression.
Example:
int day =
1;
String dayName;
switch
(day) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
// ... more cases
default:
dayName = "Invalid day";
}
System.out.println("Day:
" + dayName);
7. FUNCTIONS/METHODS:
Functions or methods are blocks of code that can be called to perform a specific task. They are used to organize code and make it reusable.
Example:
// Method
that calculates the sum of two numbers
public
static int sum(int num1, int num2) {
return num1 + num2;
}
// Calling
the method
int result
= sum(10, 20);
System.out.println("Sum:
" + result);
These
explanations, along with the provided examples, should give you a solid
understanding of these fundamental concepts in Java programming. Remember to
practice and experiment with code to further enhance your skills.
Hope you like the post. Please don't forget give you view in comment section.
Comments
Post a Comment