Learn Full JAVA Programming Course from this Cheatsheet and Execute a Project on your own
Page Content
Java language was developed by Sun Microsystems in 1995. In subsequent years, the language has become the backbone of millions of applications across multiple platforms including Windows, Macintosh, and UNIX-based desktops, Android-based mobiles, embedded systems, and enterprise solutions. According to Oracle (that acquired Sun Microsystems in 2010), Java now runs on more than 3 billion devices.
Java is one of the most popular, most adopted, and general-purpose programming language used by millions of developers and billions of devices around the world. It is used to develop all kinds of Android apps, desktop apps, and video games. It is also commonly used as a server-side language for enterprise-level backend development. This programming language has long-term compatibility and developers are comfortable with Java.
Also read: How to Learn Python Programming Language in 30 Days
Java Programming Cheatsheet:
Java Data Types
The data type specifies the size and type of values that can be stored in an identifier. The Java language is rich in its data types. Different data types allow you to select the type appropriate to the needs of the application.
Data types in Java are classified into two types:
Primitive—which include Integer, Character, Boolean, and Floating Point.
Non-primitive—which include Classes, Interfaces, and Arrays.
byte / short / int / long | 10,-30 |
float / double | 88.58 |
char | ‘D’ |
boolean | true, false |
String | “Keep Following Techworm” |
Java Data Conversions
String to Number int i = Integer.parseInt(str); double d = Double.parseDouble(str); |
Any Type to String String s = String.valueOf(value); |
Numeric Conversions int i = (int) |
Java Arithmetic Operators
x + y | add | x – y | subtract |
x * y | multiply | x / y | divide |
x % y | modulus | ++x / x++ | increment |
–x / x– | decrement |
Java Comparison Operators
x < y | Less | x <= y | Less or eq |
x > y | Greater | x >= y | Greater or eq |
x == y | Equal | x != y | Not equal |
Java Boolean Operators
! x (not) | x && y (and) | x || y (or) |
Java Text Formatting
printf style formatting System.out.printf(”Count is %d\n”, count); s = String.format(“Count is %d”, count);MessageFormat style formatting s = MessageFormat.format( ”At {1,time}, {0} Runs Scored.”, 25, new Date());Individual Numbers and Dates s = NumberFormat.getCurrency() .format(x); s = new SimpleDateFormat(“”yyyy/mm/dd””) .format(new Date()); |
Java Hello World
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } } |
Java String Methods
s.length() | length of s |
s.charAt(i) | extract ith character |
s.substring(start, end) | substring from start to end-1 |
s.toUpperCase() | returns copy of s in ALL CAPS |
s.toLowerCase() | returns copy of s in lowercase |
s.indexOf(x) | index of first occurence of x |
s.replace(old, new) | search and replace |
s.split(regex) | splits string into tokens |
s.trim() | trims surrounding whitespace |
s.equals(s2) | true if s equals s2 |
s.compareTo(s2) | 0 if equal/+ if s > s2/- if s < s2 |
Java Statements
If Statement if ( expression ) { statements } else if ( expression ) { statements } else { statements }While Loop while ( expression ) { statements }Do-While Loop do { statements } while ( expression );For Loop for ( int i = 0; i < max; ++i) { statements }For Each Loop for ( var : collection ) { statements }Switch Statement switch ( expression ) { case value: statements break; case value2: statements break; default: statements }Exception Handling try { statements; } catch (ExceptionType e1) { statements; } catch (Exception e2) { catch-all statements; } finally { statements; } |
Java Arrays
Single Dimensional (1-D)
Single Dimensional or 1-D array is a type of linear array in which elements are stored in a continuous row.
// Initializing type[] varName= new type[size]; // Declaring type[] varName= new type[]{values1, value2,...};
Creating an array with random values.
double[] arr = new double[n]; for (int i=0; i<n; i++) {a[i]= Math.random();}
Searching the max value in the array.
double max = 0; for(int i=0; i<arr.length(); i++) { if(a[i]> max) max = a[i]; }
Reversing an array.
for(int i=0; i<(arr.length())/2; i++) { double temp = a[i]; a[i]= a[n-1-i]; a[n-1-i] = temp; }
Multi-Dimensional (2-D)
Two Dimensional or 2-D array is an array of an array where elements are stored in rows and columns.
// Initializing datatype[][] varName = new dataType[row][col]; // Declaring datatype[][] varName = {{value1, value2....},{value1, value2....}..};
Transposing a matrix.
for(i = 0; i < row; i++) { for(j = 0; j < column; j++) { System.out.print(array[i][j]+" "); } System.out.println(" "); }
Multiplying two matrices.
for (i = 0; i < row1; i++) { for (j = 0; j < col2; j++) { for (k = 0; k < row2; k++) { sum = sum + first[i][k]*second[k][j]; } multiply[i][j]= sum; sum = 0; } }
Applications of Java
Java is being used in:
- Real-time Systems
- Simulation and Modelling
- object-oriented Databases
- Artificial Intelligence and Expert Systems
- CIM/CAD/CAM Systems
- Neural Networks and Parallel Programming
- Decision Support Systems
This is the full Cheatsheet for JAVA Programming, mention if we missed any in the comment box, for more Programming Cheatsheets follow Android Rookies.