As java is a object oriented programming language so everything here is considered as a combination of class and object. In the above code snippet we can see the basic skeleton of a java program. Here in -
Mark (0) - This is the importing of package or library.
Mark (1) - This is the starter point of the program.
Mark (2) - This is main function and the entry point of program execution.
Mark (3) - A basic statement which print Hello Java in console.
*** Java 25 don’t need the whole class and static void main() method as starting point. We can just write the statement and Java consider them as part of main() ***
In programming variable is like a container which can holds data of different types. As java is a strongly typed language so we need to specify the type when declare a variable.
import java.io.*;
classOperators {
publicstaticvoidmain(String[] args)
{
int a = 10, b = 20;
int c = a + b;
int d = a ^ b;
bool isSame = a == b;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(isSame);
}
}
The best way of practicing programing is solving programming problem. Lets solve the problem LeetCode 371. Sum of Two Integers by using the knowledge that we learn till now.
Problem says that given two integer a and b, calculate their sum.
import java.io.*;
publicclassIfElseExample {
publicstaticvoidmain(String[] args) {
int age = 20;
if(age>18){
System.out.print("Age is greater than 18");
}
else {
System.out.print("Age is not greater than 18");
}
}
}
Please note: infinite level of nesting of if else or else if ladder is support by the java program.
import java.io.*;
publicclassElseIfLadderExample {
publicstaticvoidmain(String[] args) {
int age = 20;
if(age>18){
System.out.print("Age is greater than 18");
if(age > 20) {
System.out.print("Age is greater than 20");
}
else {
System.out.print("Age is greater than 18 but less than 20");
}
}
elseif(age == i8){
System.out.print("Age is equal to 18");
}
else {
System.out.print("Age is not greater than 18");
}
}
}
Java switch case syntax is also similar to C++. The difference between if else and switch condition is, if condition can check the all relational condition but, switch can only check equal with case value. You can write the nested switch statements also.
import java.io.*;
classSample {
publicstaticvoidmain (String[] args) {
int num=20;
switch(num){
case 5 : System.out.println("It is 5");
break;
case 10 : System.out.println("It is 10");
break;
case 15 : System.out.println("It is 15");
break;
case 20 : System.out.println("It is 20");
break;
default: System.out.println("Not present");
}
}
};
Java support switch expression in its latest release.
import java.io.*;
classSample {
publicstaticvoidmain (String[] args) {
Day day = Day.WEDNESDAY;
int numLetters =switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
};
}
};
Break statements: The switch case example has break statements. Like its name suggests, a break statement breaks an execution block so that the below code of that block will be ignored. So if the program encounters a break statement, then it immediately breaks that block.
Arrays are non-primitive data types in Java. An array is nothing but a collection of similar types of data. That means when we need more than one similar and related data, then we should use the array. Arrays are being declared using [ ]. Also, it can be one-dimensional and multi-dimensional. Arrays can be declared in 3 ways.
import java.io.*;
classDoWhileDemo {
publicstaticvoidmain(String[] args)
{
int i = 0;
do {
System.out.println("Print statement");
i++;
} while (i < 0);
}
}
import java.io.*;
classForEachDemo{
publicstaticvoidmain(String[] arg)
{
{
int[] marks = { 125, 132, 95, 116, 110 };
for (int num : marks){
System.out.println(num);
}
}
}
}
Continue statements: Continue also to ignore the execution of the existing below code of a block, but unlike break, continue its executions for the next iterations.
A Java function, also known as a method, is a block of code or a collection of statements or a set of code grouped together to perform a certain task or operation. It is used to achieve the reusability of code. The declaration signature of a method is as below:
Access_Modifier Return_type Method_Name (Parameter_List) {
Method_body (collection of statements)
return return_value; // when void type it do not return anything}
Lets write a method for finding maximum value from a array.
import java.io.*;
classFunctionDemo{
publicstaticvoidmain(String[] arg)
{
{
int[] marks = { 125, 132, 95, 116, 110 };
// call the maximum function to get highest marksint highest_marks = maximum(marks);
System.out.println("The highest score is "+ highest_marks);
}
}
// This is our maximum finding methodpublicstaticintmaximum(int[] numbers)
{
int maxSoFar = numbers[0];
for (int num : numbers)
{
if (num > maxSoFar)
{
maxSoFar = num;
}
}
return maxSoFar;
}
}
We use cookies and technologies like Google Analytics and
Microsoft Clarity to understand how users interact with our site
and improve your experience.