Open ashi509 opened 1 year ago
public class Hello{ public static void main(String args[]){ // first java program(single Line comment ) /**
System.out.println("Hello World") } }
public is Access modifier. Hello is class name static is no need to create object.. void return type. main() there is pri-define method. System is a pri-define class. .out means output and print mean output print. comments is fully ignore by JVM.
Variable is the name of memory location. Variable can store any type of value.
1- Local variable 2-static variable 3-Instance variable.
public class Main { //static variable static String str1="Ashish"; // instance Variable Double aDouble=10.00; public static void main(String[] args) { // local variable int num=10; String str="Ramesh"; System.out.println(str1 +"\n"+str+"\n"+num); //crate instance for accessing instance variable Main obj=new Main(); System.out.println(obj.aDouble); } }
Datatype specific the different type of value that store on the variable.
Identifier is the name of Variable method ,class,object,or Interface.that not allow of java reserved keyword.
public class Main { public static void main(String[] args) { int num=10; String str="Ramesh"; System.out.println(str+"\n"+num); } }
int and String is a Datatype and num and str is a Identifier.
In java we can take User Input . in java for taking user input we are using The Scanner class is used to get user input, and it is found in the java.util package
import java.util.Scanner; // Import the Scanner class
class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Create a Scanner object System.out.println("Enter username");
String str = scanner.nextLine(); // Read user input
System.out.println("Username is: " + str); // Output user input
} }
Converting one datatype to other datatype called type casting .
1-Implicit 2-Explicit
class a{ public static void main(String args[]){ int x=10; double d=x; System.out.println(d); } }
public class Main{ public static void main(String[] args) { double x=10.66; int d=(int)x; System.out.println(d); } }
Operator is a symbol that is used to perform operation according to user requirement.
Java Operators are mainly of the following types:
Arithmetic Operators
Logical Operators
Unary Operators
Assignment Operators
Ternary Operators
Relational Operators
Bitwise Operators
Shift Operators
instanceOf operator
public class Main{ public static void main(String[] args) { // Arithmetic operator double x=10.0d; double y=5.0d; double z=x+y; double s=x-y; double t=x*y; double u=x/y; double g=x%y; System.out.println("Addition of number "+z); System.out.println("subtraction of number "+s); System.out.println("multiplication of number "+t); System.out.println("divide of number "+u); System.out.println("module of number "+g);
//relational operator // And or operator System.out.println(x>y); System.out.println(x<y); System.out.println(x<=y || x>=y); System.out.println(x>y && y<x);
/**
* increment or decrement operator
*/
System.out.println(x++);
System.out.println(++x);
System.out.println(y--);
System.out.println(--y);
/**
Java provides three types of control flow statements.
if statements
1-simple if
2-if else
3 if else-if(ladder if else)
switch statement
do while loop
while loop
for loop
for-each loop
break statement
continue statement
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y > 20) {
System.out.println("x + y is greater than 20");
}
}
}
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
int x=x+y;
if(z > 20) {
System.out.println("x + y is greater than 20");
} else{
System.out.println("x+y is less than 20");
}
}
}
Introduction Of Java
what is java ?
Java is an object oriented programing language.Developed by "James Gosling" and his team in 1991. first version of java release in 1995. Java first name id "Oak".
feature of java :--
1- java is a platform independent language. 2- java provides "Write once run any where".
Flaour of java.
1-J2SE - Core java 2-J2EE- Advance Java 3- J2ME-Android Java.