Core Java Cheat Sheet – Basics Of Java Programming
Last updated on Jun 14,2019 18.9K Views
Swatee Chand
Research Analyst at Edureka. A techno freak who likes to explore di erent...
Are you an aspiring Java developer? Well, if you are, then I bet you can make use of this Java Cheat Sheet. Java is known for its
pre-built classes and libraries and sometimes, keeping a track of them becomes a little tricky. So, here I bring you the Core Java
Cheat Sheet.
This cheat sheet will act as a crash course for Java beginners and help you with various fundamentals of Java.
Core Java Cheat Sheet
Java is an open source programming language that has been changing the face of
the IT market since ages. It is widely preferred by the programmers as the code
written in Java can be executed securely on any platform, irrespective of the
operating system or architecture of the device. The only requirement is, Java
Runtime Environment (JRE) installed on the system.
Primitive Data Types Java Operators
Let’s start o by learning the primitive data types that Java There are mainly 8 di erent types of operators available in
o ers: Java:
Data Operator Type Operators
Size Range
Type
Arithmetic +, – , *, ? , %
byte 8 -128..127
=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=,
Assignment
short 16 -32,768..32,767 >>>=
int 32 -2,147,483,648.. 2,147,483,647 Bitwise ^, &, |
-9,223,372,036,854,775,808.. Logical &&, ||
long 64
9,223,372,036,854,775,807
Relational <, >, <=, >=,==, !=
oat 32 3.4e-0.38.. 3.4e+0.38
Shift <<, >>, >>>
double 64 1.7e-308.. 1.7e+308
Ternary ?:
char 16 Complete Unicode Character Set
Unary ++x, –x, x++, x–, +x, –x, !, ~
Boolean 1 True, False
Java Variables Java Methods
Variables in Java refers to the name of the reserved memory A method is a set of code that is grouped together to
area. You need variables to store any value for the perform a speci c operation. A method is completed in two
computational or reference purpose. steps:
There are 3 types of variable in Java: 1. Method Initialization
2. Method Invocation
1. Local Variables
2. Instance Variables A method can be invoked either by calling it by reference or
3. Static Variables by value.
{public | private} [static] type name [=
expression | value]; {public | private} [static] {type | void}
name(arg1, ..., argN ){statements}
Data Conversion User Input
The process of changing a value from one data type to Java provides three ways to take an input from the user/
another type is known as data type conversion. Data Type console:
conversion is of two types:
1. Using Bu erReader class
1. Widening: The lower size datatype is converted into a 2. Using Scanner class
higher size data type without loss of information. 3. Using Console class
2. Narrowing: The higher size datatype is converted into a
lower size data type with a loss of information.
// Widening // Using BufferReader
(byte<short<int<long<float<double) BufferedReader reader = new
int i = 10; //int--> long BufferedReader(new
long l = i; //automatic type conversion InputStreamReader(System.in));
// Narrowing String name = reader.readLine();
double d = 10.02;
long l = (long)d; //explicit type casting // Using Scanner
// Numeric values to String Scanner in = new Scanner(System.in);
String str = String.valueOf(value); String s = in.nextLine();
// String to Numeric values int a = in.nextInt();
int i = Integer.parseInt(str);
double d = Double.parseDouble(str); // Using Console
String name = System.console().readLine();
Basic Java Program Compile a Java Program
A basic program in Java will consist of at least the following You need to save your Java Program by the name of the
components: class containing main() method along with .java extension.
1. Classes & Objects className.java
2. Methods
3. Variables Call the compiler using javac command.
javac className
public class Demo{ Finally, execute the program using below code:
public static void main(String[] args)
java className
{ System.out.println("Hello from
edureka!");}
}
Flow Of Control
Iterative Statements Decisive Statements
Iterative statements are used when you need to repeat a set Selection statements used when you need to choose
of statements until the condition for termination is not met. between alternative actions during execution of the program.
// for loop //if statement
for (condition) {expression} if (condition) {expression}
// for each loop //if-else statement
for (int i: someArray) {} if (condition) {expression} else {expression}
// while loop //switch statement
while (condition) {expression} switch (var)
// do while loop { case 1: expression; break; default:
do {expression} while(condition) expression; break; }
Generating a Fibonacci series. Checking the given number is prime or not.
for (i = 1; i <= n; ++i)
{System.out.print(t1 + " + "); if (n < 2) { return false; }
int sum = t1 + t2; for (int i=2; i <= n/i; i++)
t1 = t2; {if (n%i == 0) return false;}
return true;
t2 = sum;}
Creating a pyramid pattern. Finding the factorial using recursion function.
k = 2*n - 2; int factorial(int n)
for(i=0; i<n; i++) {
{ for(j=0; j<k; j++){System.out.print(" ");} if (n == 0)
k = k - 1; {return 1;}
for(j=0; j<=i; j++ ){System.out.print("* ");} else
System.out.println(); } {return(n * factorial(n-1));}
}
Java Arrays
Single Dimensional (1-D) Multi Dimensional (2-D)
Single Dimensional or 1-D array is a type of linear array in Two Dimensional or 2-D array is an array of an array where
which elements are stored in a continuous row. elements are stored in rows and columns.
// Initializing // Initializing
type[] varName= new type[size]; datatype[][] varName = new dataType[row]
[col];
// Declaring
type[] varName= new type[]{values1, // Declaring
value2,...}; datatype[][] varName = {{value1,
value2....},{value1, value2....}..};
Creating an array with random values.
Transposing a matrix.
double[] arr = new double[n];
for (int i=0; i<n; i++) for(i = 0; i < row; i++)
{a[i] = Math.random();} { for(j = 0; j < column; j++)
{ System.out.print(array[i][j]+" "); }
Searching the max value in the array. System.out.println(" ");
}
double max = 0;
for(int i=0; i<arr.length(); i++)
{ if(a[i] > max) max = a[i]; } Multiplying two matrices.
for (i = 0; i < row1; i++)
Reversing an array.
{ for (j = 0; j < col2; j++)
for(int i=0; i<(arr.length())/2; i++) { for (k = 0; k < row2; k++)
{ double temp = a[i]; { sum = sum + first[i][k]*second[k][j];
a[i] = a[n-1-i]; }
a[n-1-i] = temp; multiply[i][j] = sum;
} sum = 0;
}
}
Java Strings
Creating a String String Methods
String in Java is an object that represents a sequence of char Few of the most important and frequently used String
values. A String can be created in two ways: methods are listed below:
1. Using a literal
2. Using ‘new’ keyword str1==str2 //compares address;
String newStr = str1.equals(str2); //compares
String str1 = “Welcome”; // Using literal the values
String newStr = str1.equalsIgnoreCase()
String str2 = new String(”Edureka”); // Using //compares the values ignoring the case
new keyword newStr = str1.length() //calculates length
newStr = str1.charAt(i) //extract i'th
character
The java.lang.String class implements Serializable,
newStr = str1.toUpperCase() //returns string
Comparable and CharSequence interfaces. Since the String in ALL CAPS
object is immutable in nature Java provides two utility classes: newStr = str1.toLowerCase() //returns string
in ALL LOWERvCASE
1. StringBu er: It is a mutable class that is thread-safe and
newStr = str1.replace(oldVal, newVal)
synchronized.
//search and replace
2. StringBuilder: It is a mutable class that is not thread-safe
newStr = str1.trim() //trims surrounding
but is faster and is used in a single threaded
whitespace
environment.
newStr = str1.contains("value"); //check for
the values
newStr = str1.toCharArray(); // convert
String to character type array
newStr = str1.IsEmpty(); //Check for empty
String
newStr = str1.endsWith(); //Checks if string
ends with the given suffix
Download Core Java Cheat Sheet for Beginners Edureka
Recommended videos for you
Hibernate Mapping on the Fly PHP and MySQL : Server Side Scripting Microsoft Sharepoint 2013 : The
For Web Development Ultimate Enterprise Collaboration
Platform
Watch Now Watch Now Watch Now
Recommended blogs for you
How To Implement Static Variable In Top Data Structures & Algorithms in How To Implement Java Composition
C? Java That You Need to Know In Depth?
Read Article Read Article Read Article
Comments 1 Comment
2 Comments https://www.edureka.co/blog/
1 Login
Recommend t Tweet f Share Sort by Best
Join the discussion…
LOG IN WITH OR SIGN UP WITH DISQUS ?
Name
Ulan Yisaev
2 months ago
− ⚑
Thanks a lot!
△ ▽ Reply
Sunil Poudel
− ⚑
a year ago edited
I am searching this kind of stuff for years.... Now I found it. Many thanks.
△ ▽ Reply
ALSO ON HTTPS://WWW.EDUREKA.CO/BLOG/
AWS Snowball and Snowmobile Tutorial Cybersecurity Firewall: How Application Security Works?
1 comment • 3 months ago 1 comment • 3 months ago
Excelanto Cloud Systems — Excellent information with unique Hackers Guru — Before launching any app, the develop should
Avatarcontent and it is very useful to know about the information based Avatarconduct a penetration test to find vulnerabilites.
on blogs.
Top 75 Digital Marketing Interview Questions and … Android Services Tutorial : How to run an application in the …
2 comments • 3 months ago 1 comment • 3 months ago
Sam — Great content, very beneficial for me. Julia Johnson — It’s a Nice Blog and also Gets Complete
AvatarAlso read this blog for more such questions with answers Avatarinformation about android app. https://www.iqlance.com/new...
https://treehack.com/digita...
✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Disqus' Privacy PolicyPrivacy PolicyPrivacy
Trending Courses in Programming & Frameworks
Java, J2EE & SOA Certi cation Training Python Scripting Certi cation Training
34k Enrolled Learners 8k Enrolled Learners Python Django Training and
Weekend Weekend
Certi cation
Live Class Self Paced 4k Enrolled Learners
Reviews Reviews Weekend
Live Class
4 (13500) 5 (2850)
Reviews
5 (1250)
Browse Categories
Arti cial Intelligence BI and Visualization Big Data Blockchain Cloud Computing Cyber Security Data Science
Data Warehousing and ETL Databases DevOps Digital Marketing Front End Web Development Mobile Development
Operating Systems Project Management and Methodologies Robotic Process Automation Software Testing Systems & Architecture
TRENDING CERTIFICATION COURSES TRENDING MASTERS COURSES
DevOps Certi cation Training Data Scientist Masters Program
AWS Architect Certi cation Training DevOps Engineer Masters Program
Big Data Hadoop Certi cation Training Cloud Architect Masters Program
Tableau Training & Certi cation Big Data Architect Masters Program
Python Certi cation Training for Data Science Machine Learning Engineer Masters Program
Selenium Certi cation Training Full Stack Web Developer Masters Program
PMP® Certi cation Exam Training Business Intelligence Masters Program
Robotic Process Automation Training using UiPath Data Analyst Masters Program
Apache Spark and Scala Certi cation Training Test Automation Engineer Masters Program
Microsoft Power BI Training Post-Graduate Program in Arti cial Intelligence & Machine Learning
Online Java Course and Training Post-Graduate Program in Big Data Engineering
Python Certi cation Course
COMPANY WORK WITH US
About us Careers
News & Media Become an Instructor
Reviews Become an A liate
Contact us Become a Partner
Blog Hire from Edureka
Community
DOWNLOAD APP
Sitemap
Blog Sitemap
Community Sitemap
Webinars
CATEGORIES
TRENDING BLOG ARTICLES
© 2019 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved. Terms & Conditions
Legal & Privacy
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of
MongoDB, Inc.