0% found this document useful (0 votes)
4 views

JAVA Notes

Uploaded by

creatorcoder268
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

JAVA Notes

Uploaded by

creatorcoder268
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

JAVA

Java is an object oriented programming language which is developed by Sun Microsystem . That produces software for multiple platforms .
When a programmer writes a Java application , the compiled code (known as bytecode) runs on most os , including , window , linux & Mac
Os .Java is a popular programming language, created in 1995. It is used for Mobile applications (specially Android apps) , Desktop applications ,
Web applications , Web servers and application servers , Games , Database connection .

Note : file name and java main class should be same and every code should be inside main class . java is case-sensative lang .

Create file as filename.java

Syntax :
1. Public class filename{

2. Public static void main(String[] args){

3. System.out.println(“…….”);

4. }

5. }

Explanation :
Line 1 : public keyword made java class accessible easily to every other java program , class keyword use to declare class with its
name , { indicate stating of class body .

Line 2 : public keyword make main method accessible , static keyword indicate that the main merthod is belong to its respective class
, void indicate its return type if use void no return statement is need at the end of code to terminate it , main() is main method that
carries all functionality of its class no prog is cmplt without main method becoz processing is always done in main method only ,
String[] args It stores Java command-line arguments and is an array of type java.lang.String class. Here, the name of the String array
is args but it is not fixed and the user can use any name in place of it. { indicate starting of main method .

Line 3 : System is a built-in Java class that contains useful members, such as out, which is short for "output". The println() method,
short for "print line", is used to print a value to the screen (or a file).

Line 4 : } indicates end of main method body , line 5 : } indicates end of java class body .

Note : to print character , alphabets or statements , symbols , code it inside double quotes (“ “) but to print numbers and variable no need to
use double quotes , to concatenate two statements or numbers use + sign .

To comment out statement use :

// for single line comment and /* ….. */ for multi line comments

Variable syntax : type variablename = value ;

Data types : int , float , double , char , Boolean , long

Operators :- Arithmetic operators : + , - , * , / , % , ++ , --

Assignment operators : = , += , -= , *= /= , %= ,&= , |= , >>= , <<=

Comparison operators : == , != , > , < , >= , <=

Logical operators : && , || , !

User Input :
syntax : import.java.util.* or import.java.util.Scanner ;

Public class classname {

Public static void main(String[] args){


Scanner objectname = new Scanner(System.in);

System.out.println(“Enter Statement : “ ):

Datatype variablename = objectname.nextDatatypeName ; } }

Important Points About Java Scanner Class

 To create an object of Scanner class, we usually pass the predefined object System.in, which represents the standard input stream. We
may pass an object of class File if we want to read input from a file.

 To read numerical values of a certain data type XYZ, the function to use is nextXYZ(). For example, to read a value of type short, we
can use nextShort()

 To read strings, we use nextLine().

 To read a single character, we use next().charAt(0). next() function returns the next token/word in the input as a string and charAt(0)
function returns the first character in that string.

 The Scanner class reads an entire line and divides the line into tokens. Tokens are small elements that have some meaning to the Java
compiler. For example, Suppose there is an input string: How are you
In this case, the scanner object will read the entire line and divides the string into tokens: “How”, “are” and “you”. The object then
iterates over each token and reads each token using its different methods.

Maths : The Java Math class has many methods that allows you to perform mathematical tasks on numbers.
Math.max(value1 , value2) , Math.min(value1 , value2) , Math.sqrt(value) , Maths.abs(value) and much more .

Maths Methods :
Syntax of above Methods :

i. Math.abs( value );
ii. Math.acos( value );
iii. Math.addExact( value1 , value2 );
iv. Math.asin( value );
v. Math.atan( value );
vi. Math.atan2( value1 , value2 );
vii. Math.cbrt( value );
viii. Math.ceil( value );
ix. Math.copySign( value1 , value2 );
x. Math.cos( value );
xi. Math.cosh( value );
xii. Math.decrementExact( value );
xiii. Math.exp( value );
xiv. Math.expm( value );
xv. Math.floor ( value );
xvi. Math.floorDiv( value1 , value2 );
xvii. Math.floorMod( value1 , value2 );
xviii. Math.getExponent( value );
xix. Math.hypot( value1 , value2 );
xx. Math.incrementExact( value );
xxi. Math.log( value );
xxii. Math.log10( value );
xxiii. Math.max( value1 , value2 );
xxiv. Math.min( value1 , value2 );
xxv. Math.multiplyExact( value1 , value2 );
xxvi. Math.negateExact(value);
xxvii. Math.nextAfter(value1 , value2);
xxviii. Math.nextDown(Value);
xxix. Math.nextUp(value);
xxx. Math.pow(value1 , value2);
xxxi. Math.random();
xxxii. Math.rint(value);
xxxiii. Math.round(value);
xxxiv. Math.scalb(value1 , value2);
xxxv. Math.signum(value);
xxxvi. Math.sin(value);
xxxvii. Math.sinh(value);
xxxviii. Math.sqrt(value);
xxxix. Math.subtractExact(value1 ,value2);
xl. Math.tan(value);
xli. Math.tanh(value);
xlii. Math.toDegree(value);
xliii. Math.toIntExact(value);
xliv. Math.toRadians(value);
xlv. Math.ulp(value);

String :
A String variable contains a collection of characters surrounded by double quotes

Syntax : String variablename = “value”;

variablename.length() to identify string length , variablename.toUpperCase() to convert string in capitalized letter ,


variablename.toLowerCase() to convert string in small letter and much more .

String Methods :

Syntax of Above Methods : let strings are str & str2 ,

i. str.charAt(index);
ii. str.codePointAt(index);
iii. str.compareTo(str2);
iv. str.concat(str2);
v. str.contains(“…..”);
vi. str.endsWith(“…..”);
vii. str.equals(str2);
viii. str.codePointCount(startIndex , endIndex);
ix. str.hashCode();
x. str.indexOf(“……”);
xi. str.isEmpty( );
xii. str.join(“……” , “…….” , ”……”);
xiii. str.LastIndaexOf(“……”);
xiv. str.length( );
xv. str.replace(“existCharacter” , “newCharacter” );
xvi. str.startsWith(“ …… “);
xvii. str.toCharArray( );
xviii. str.toLowerCase( );
xix. str.toUpperCase( );
xx. str.toString( );
xxi. str.trim( );
xxii. str.valueOf( );

Condition Statements :
If :

Syntax : if(condition){ statement…. }

Nested if :

Syntax: if(condition1){

If(condition2){ statement….}

If…else :

Syntax : if(condition){ statement…. }

else{ statement….}

if…..else if ….. else :

syntax : if(condition1){ statement…. }

else if (condition2){ statement….}

else{ statement….}

ternary operators :

syntax : variable = (condition) ? expressionTrue : expressionFalse;

switch case :

syntax : switch(expression) {
case x:
statement
break;
case y:
statement
break;
default:
statement
}
Note : break keyword used to jump out from whole condition or loop but continue keyword to its specific condition or loop and again continue
from true condition and loop .

Looping :
Loops can execute a block of code as long as a specified condition is reached.

While loop :

Syntax : initialize variable

while (condition){

Statement….

Increment/decrement }
Do-While loop :

Syntax : initialize variable

do{ statement …

increment/decrement

} while(condition)

For Loop :

Syntax : for(initialize variable , condition , increment/decrement ){

Statement … }

Array : Arrays are used to store multiple values of same datatype in a single variable .
Syntax : datatype[] variablename = { value1 , value2, ……. , valueN};

For check array length : variablename.length

For display array element : for(initialize loop limits , condition , increment/decrement ){

System.out.println(variablename[i]); }

For each-loop : syntax : for ( datatype variablename : arrayname ){

System.out.println(variablename): }

Here variable name work as index of array .

Multidimentional array :

Syntax : datatype[][] variablename = { { value1 , value2 , …..,valueN } , {valueA , valueB , …. , valueN}};

Access element :

Syntax : for(initialise row limit , condition , increment/decrement ){

For(initialise column limit , condition , increment/decrdement ){

System.out.println(arrayname[row][column]);}}

User defined array :

Public class className{

Public static void main(String[] args){

Scanner sc = new Scanner(System.in);

System.out.println("Enter no.of array element :\n");


int n = sc.nextInt();
int[] a = new int[10];
int i;

System.out.println("Enter number :");


for(i=0 ; i<n ; i++){
a[i]=sc.nextInt();
}

System.out.println("Array elements :");


for(i=0 ; i<n ; i++){
System.out.println(a[i]);
}
sc.close();
}}

You might also like