SlideShare a Scribd company logo
Java programming cheat sheet
Data Type Conversion
Narrowing
Widening
Casting
promotion
Assignment
The desired types
are placed inside ()
int y=(int)1.5;
Binary operation
involving two operands
of different types
double y=2 +1.5;
One type to
another type
int x=2;
double y=x;
Console input: Use Scanner Class
Console output
1-import declaration
import java.util.Scanner;
2-Scanner object with data source
Scanner input=new Scanner (System.in);
3-Scanner methods:
DataTypenameVariable=ObjectScanner.nextDataType();
String s=input.next(); //read a word | String s=input.nextLine(); //read a line
char c=input.next().charAt(0);
"+0);
ends a line of output
("
.println
System.out
"+0);
doesn't ends a line of output
("
.print
System.out
(+) used to connect more than one item
System.out.printf("format String", variable list);
5.147300
System.out.printf("%f%n", 5.1473); //output:
Control flow statement
switch - case
Nested if
Ternary operator
? :
if-else
if
Selection (Decision):
Choose between two
or more possible
action based on some
condition.
switch(expression){
case x:
// code block
break;
default:
// code block }
if(condition1){
//Executes when
the condition1 is true
}
if(condition2){
Executes
when the condition2 is
true}
It's shorthand of
if-else
condition?True:False
if (condition) {
//code executed if
the condition is
true}
else {
// code executed if
the condition is
false}
if (condition)
{
// block of code to be executed if the condition is
true
}
Nested loop
do-while: post test
while: pre test
for: pre test
Repetition(iteration):
Repeats a group of
actions until a
stopping condition
occurs.
for(initial action ;condition;
action after each loop){
for(initial action ;condition;
action after each loop){
//statement of inner }
// statement of outer}
Control variable = initial
value;
do{ //statement
Update controlvariable; }
while (condition)
Control variable = initial value;
while (condition){
//statement
Update control variable;
}
for(initial action;condition; action after each loop)
{
//statement
}
return: using in body of methods
continue
break
Branching(jump):
Interrupts the flow of
actions.
Return Type methods
Void
Labeled continue
Skips the current
iteration of the
inner most (for,
while, do-
while)loop
Labeled break
Terminate the inner
most(Switch,for,while,
do-while)statement
Require one or more return
statement
Method header (parameters){
method body
return expression ;}
Doesn't require a return
statement
loop:
for(int i=0;i<2;i++){
for(int j=0;j<5;j++)
if(j==2)
continue loop;}
loop:
for(int i=0;i<2;i++){
for(int j=0;j<5;j++)
if(j==2)
break loop;}
.
is a value that cannot be changed after assigning it
Constant:
= value;
identifier_name
datatype
final
Syntax:
=3.14;
PI
double
final
For ex:
Methods of Array
Methods of Math
Methods of String
Methods
import java.util.Arrays;
int a=Arrays.copyOfRange(a,0,2);
Arrays.sort(a);
boolean b=Arrays.equals(a,newA);
int x=Arrays.binarysearch(a,2);
Math.pow(a,b); //a^b
Math.abs( x );
Math.min( a, b );
Math.max( a, b );
Math.round( x );
Math.ceil( x );
Math.floor( x );
Math.sqrt( x );
a+(int)Math.random( )*b;
//return a random numbers
between a and a+b exclusive
//compare address
boolean b= s1==s2;
//compare value
String s=s1.equals(s2);
String s=s1.equalsIgnoreCase(s2);
String s=s1.toupperCase();
String s=s1.tolowerCase();
String s=s1.trim();
String s=s1.subString(start value);
Char c=s1.charAt(0);
int l=s1.length();
int l=s1.indexOf("String");
int l=s1.compareTo("String");
int l=s1.compareToIgnoreCase();
public int max (int x ,int y)
{
If(x<y)
return x;
else
return y;
}
Operator:
1.Parenthaces
2.Unary:
++
--
+
-
!
Cast
3.Arithametic
:
*
/
%
+
-
4.Compretion
:
<
<=
>
>=
==
!=
5.logical
&
^
|
&&
||
6.Ternary:
? :
7.Assginment:
=
+=
-=
*=
/=
%=
comments in java: // single line , /* multiline */, /** Javadoc*/
Global variable
(instance variable)
Local variable
it's defined within the
class but not inside the
body of any method.
it's accessible to all
methods of the class
Declare in the
body of the
method, can be
used only in the
method
Public class test{
int a;
Public void main(){
int b; }}
Array
Two dimensions
One dimension
datatype [][] arrName = new datatype [R_siza][C_size];
datatype [] arrName = new datatype [];
Syntax
int [][] a= new int [][]{{1,2},{3,4},{5,6}};
int[]a=new int[]{1,2};
Declaration,
creation,
initializing
Col| a[0].length ? 2
Rows| a.length; ? 3
a.length; ? 2
Length
for (int i=0; i<a.length; i++) {
for (int j=0; j<a.length; j++) {
System.out.println(aa[i][j]+""); }
System.out.println();
}
for (int i = 0; i < arr.length; i++){
System.out.println(arr[i] + " ");
} // printing array element
print
Main Data Type
Reverence
primitive
1-classes
2-Arrays
3-String
Non numeric
numeric
1-char
(16 bit)
2-boolean
(true/false)
Integers:
1- byte(8bit)
2-short(16bit)
3-int(32bit)
4-long(64bit)
Floating point:
1-float(32bit)
2-double(64bit)
Body of the method
Parameter list
Method name
Return type
modifier

More Related Content

PDF
Dynamic Programming and Reinforcement Learning applied to Tetris Game
PPT
6 spatial filtering p2
PDF
게임 개발에 자주 사용되는 디자인 패턴
PDF
Saliency Detection via Divergence Analysis: A Unified Perspective ICPR 2012
PDF
Programming for Problem Solving
PPTX
Passing an Array to a Function (ICT Programming)
PPTX
PDF
Arrays In Python | Python Array Operations | Edureka
Dynamic Programming and Reinforcement Learning applied to Tetris Game
6 spatial filtering p2
게임 개발에 자주 사용되는 디자인 패턴
Saliency Detection via Divergence Analysis: A Unified Perspective ICPR 2012
Programming for Problem Solving
Passing an Array to a Function (ICT Programming)
Arrays In Python | Python Array Operations | Edureka

What's hot (20)

PPTX
Machine Learning on Your Hand - Introduction to Tensorflow Lite Preview
PDF
A Comparison of Block-Matching Motion Estimation Algorithms
PPT
RECURSION IN C
PPTX
04 Multi-layer Feedforward Networks
PPTX
What is Constructors and Destructors in C++ (Explained with Example along wi...
PPT
Binary Search
PPTX
Computer Vision Introduction
PPTX
operator overloading & type conversion in cpp
PPTX
HOPFIELD NETWORK
PPT
Image processing7 frequencyfiltering
PPTX
Soft computing01
PDF
Reinforcement Learning
PPTX
Local variables Instance variables Class/static variables
PPTX
Python Exception Handling
PPTX
Generators In Python
PDF
C Programming Assignment
PPTX
딥러닝 - 역사와 이론적 기초
PPT
Fibonacci search
PPTX
Comparison of Learning Algorithms for Handwritten Digit Recognition
PPTX
Image Sampling and Quantization.pptx
Machine Learning on Your Hand - Introduction to Tensorflow Lite Preview
A Comparison of Block-Matching Motion Estimation Algorithms
RECURSION IN C
04 Multi-layer Feedforward Networks
What is Constructors and Destructors in C++ (Explained with Example along wi...
Binary Search
Computer Vision Introduction
operator overloading & type conversion in cpp
HOPFIELD NETWORK
Image processing7 frequencyfiltering
Soft computing01
Reinforcement Learning
Local variables Instance variables Class/static variables
Python Exception Handling
Generators In Python
C Programming Assignment
딥러닝 - 역사와 이론적 기초
Fibonacci search
Comparison of Learning Algorithms for Handwritten Digit Recognition
Image Sampling and Quantization.pptx
Ad

Similar to java programming cheatsheet (20)

PPTX
Revision of introduction in java programming.pptx
PDF
Java q ref 2018
PPTX
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
PPTX
UNIT 2 programming in java_operators.pptx
PPTX
Nalinee java
PPTX
Lecture 3 and 4.pptx
PPTX
JPC#8 Introduction to Java Programming
PDF
Week 4
PDF
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
PPTX
Java chapter 2
PPTX
For Loops and Variables in Java
PPTX
Java_Class_1_Basics_Questions_Solutions (1).pptx
PDF
Java Cheat Sheet
PPTX
Java_Class_1_Corporate_Theme_javabasic .pptx
PPTX
Pi j1.3 operators
PPT
Data types and Operators
PPTX
3- Operators in Java
PPTX
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
PPT
9781111530532 ppt ch04
Revision of introduction in java programming.pptx
Java q ref 2018
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
UNIT 2 programming in java_operators.pptx
Nalinee java
Lecture 3 and 4.pptx
JPC#8 Introduction to Java Programming
Week 4
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
Java chapter 2
For Loops and Variables in Java
Java_Class_1_Basics_Questions_Solutions (1).pptx
Java Cheat Sheet
Java_Class_1_Corporate_Theme_javabasic .pptx
Pi j1.3 operators
Data types and Operators
3- Operators in Java
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
9781111530532 ppt ch04
Ad

Recently uploaded (20)

PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Introduction and Scope of Bichemistry.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PDF
Business Ethics Teaching Materials for college
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Open folder Downloads.pdf yes yes ges yes
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Insiders guide to clinical Medicine.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Introduction and Scope of Bichemistry.pptx
Pharma ospi slides which help in ospi learning
O7-L3 Supply Chain Operations - ICLT Program
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
NOI Hackathon - Summer Edition - GreenThumber.pptx
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
Business Ethics Teaching Materials for college
102 student loan defaulters named and shamed – Is someone you know on the list?
Open folder Downloads.pdf yes yes ges yes
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Revamp in MTO Odoo 18 Inventory - Odoo Slides
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Insiders guide to clinical Medicine.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

java programming cheatsheet

  • 1. Java programming cheat sheet Data Type Conversion Narrowing Widening Casting promotion Assignment The desired types are placed inside () int y=(int)1.5; Binary operation involving two operands of different types double y=2 +1.5; One type to another type int x=2; double y=x; Console input: Use Scanner Class Console output 1-import declaration import java.util.Scanner; 2-Scanner object with data source Scanner input=new Scanner (System.in); 3-Scanner methods: DataTypenameVariable=ObjectScanner.nextDataType(); String s=input.next(); //read a word | String s=input.nextLine(); //read a line char c=input.next().charAt(0); "+0); ends a line of output (" .println System.out "+0); doesn't ends a line of output (" .print System.out (+) used to connect more than one item System.out.printf("format String", variable list); 5.147300 System.out.printf("%f%n", 5.1473); //output: Control flow statement switch - case Nested if Ternary operator ? : if-else if Selection (Decision): Choose between two or more possible action based on some condition. switch(expression){ case x: // code block break; default: // code block } if(condition1){ //Executes when the condition1 is true } if(condition2){ Executes when the condition2 is true} It's shorthand of if-else condition?True:False if (condition) { //code executed if the condition is true} else { // code executed if the condition is false} if (condition) { // block of code to be executed if the condition is true } Nested loop do-while: post test while: pre test for: pre test Repetition(iteration): Repeats a group of actions until a stopping condition occurs. for(initial action ;condition; action after each loop){ for(initial action ;condition; action after each loop){ //statement of inner } // statement of outer} Control variable = initial value; do{ //statement Update controlvariable; } while (condition) Control variable = initial value; while (condition){ //statement Update control variable; } for(initial action;condition; action after each loop) { //statement } return: using in body of methods continue break Branching(jump): Interrupts the flow of actions. Return Type methods Void Labeled continue Skips the current iteration of the inner most (for, while, do- while)loop Labeled break Terminate the inner most(Switch,for,while, do-while)statement Require one or more return statement Method header (parameters){ method body return expression ;} Doesn't require a return statement loop: for(int i=0;i<2;i++){ for(int j=0;j<5;j++) if(j==2) continue loop;} loop: for(int i=0;i<2;i++){ for(int j=0;j<5;j++) if(j==2) break loop;} . is a value that cannot be changed after assigning it Constant: = value; identifier_name datatype final Syntax: =3.14; PI double final For ex: Methods of Array Methods of Math Methods of String Methods import java.util.Arrays; int a=Arrays.copyOfRange(a,0,2); Arrays.sort(a); boolean b=Arrays.equals(a,newA); int x=Arrays.binarysearch(a,2); Math.pow(a,b); //a^b Math.abs( x ); Math.min( a, b ); Math.max( a, b ); Math.round( x ); Math.ceil( x ); Math.floor( x ); Math.sqrt( x ); a+(int)Math.random( )*b; //return a random numbers between a and a+b exclusive //compare address boolean b= s1==s2; //compare value String s=s1.equals(s2); String s=s1.equalsIgnoreCase(s2); String s=s1.toupperCase(); String s=s1.tolowerCase(); String s=s1.trim(); String s=s1.subString(start value); Char c=s1.charAt(0); int l=s1.length(); int l=s1.indexOf("String"); int l=s1.compareTo("String"); int l=s1.compareToIgnoreCase(); public int max (int x ,int y) { If(x<y) return x; else return y; } Operator: 1.Parenthaces 2.Unary: ++ -- + - ! Cast 3.Arithametic : * / % + - 4.Compretion : < <= > >= == != 5.logical & ^ | && || 6.Ternary: ? : 7.Assginment: = += -= *= /= %= comments in java: // single line , /* multiline */, /** Javadoc*/ Global variable (instance variable) Local variable it's defined within the class but not inside the body of any method. it's accessible to all methods of the class Declare in the body of the method, can be used only in the method Public class test{ int a; Public void main(){ int b; }} Array Two dimensions One dimension datatype [][] arrName = new datatype [R_siza][C_size]; datatype [] arrName = new datatype []; Syntax int [][] a= new int [][]{{1,2},{3,4},{5,6}}; int[]a=new int[]{1,2}; Declaration, creation, initializing Col| a[0].length ? 2 Rows| a.length; ? 3 a.length; ? 2 Length for (int i=0; i<a.length; i++) { for (int j=0; j<a.length; j++) { System.out.println(aa[i][j]+""); } System.out.println(); } for (int i = 0; i < arr.length; i++){ System.out.println(arr[i] + " "); } // printing array element print Main Data Type Reverence primitive 1-classes 2-Arrays 3-String Non numeric numeric 1-char (16 bit) 2-boolean (true/false) Integers: 1- byte(8bit) 2-short(16bit) 3-int(32bit) 4-long(64bit) Floating point: 1-float(32bit) 2-double(64bit) Body of the method Parameter list Method name Return type modifier