ashi509 / Learning_Every_Thing

0 stars 0 forks source link

Learn Java #1

Open ashi509 opened 1 year ago

ashi509 commented 1 year ago

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.

ashi509 commented 1 year ago

First program in java

public class Hello{ public static void main(String args[]){ // first java program(single Line comment ) /**

System.out.println("Hello World") } }

where

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.

ashi509 commented 1 year ago

Variables in java

Variable is the name of memory location. Variable can store any type of value.

Types of Variables

1- Local variable 2-static variable 3-Instance variable.

Example

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); } }

ashi509 commented 1 year ago

Datatype

Datatype specific the different type of value that store on the variable.

image

ashi509 commented 1 year ago

Identifier

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); } }

where

int and String is a Datatype and num and str is a Identifier.

User Input

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

} }

ashi509 commented 1 year ago

Java Type casting

Converting one datatype to other datatype called type casting .

Type of type casting

1-Implicit 2-Explicit

Example

implicit

class a{ public static void main(String args[]){ int x=10; double d=x; System.out.println(d); } }

Explicit

public class Main{ public static void main(String[] args) { double x=10.66; int d=(int)x; System.out.println(d); } }

ashi509 commented 1 year ago

Java Operators

Operator is a symbol that is used to perform operation according to user requirement.

Types of operators

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

image

Example

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);

/**

ashi509 commented 1 year ago

Control statement

Java provides three types of control flow statements.

Decision Making statements

    if statements
       1-simple if
       2-if else
       3 if else-if(ladder if else)
    switch statement

Loop statements

    do while loop
    while loop
    for loop
    for-each loop

Jump statements

    break statement
    continue statement

Simple If

image 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");
}
}
}

If_else

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"); } }
}

If else-if