This document provides a summary of key Java programming concepts in 3 sentences or less:
Java programming concepts covered include data types and conversions, control flow statements like if/else and loops, methods, arrays, and classes. Core data types include primitives like int and double as well as reference types like classes, arrays, and Strings. The document also discusses operators, variables, comments, and how to declare and call methods.
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