0% found this document useful (0 votes)
13 views2 pages

Java Cheatsheet (1) - 2-3

The document provides an overview of basic Java syntax including class and method declarations, primitive and non-primitive data types, and variables. A class declaration begins with the class keyword followed by the class name. Methods can be declared within a class and given access specifiers like public. Primitive data types include integers, floats, chars, and more. Non-primitive types are arrays, strings, classes, and interfaces.

Uploaded by

sankalpk261
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Java Cheatsheet (1) - 2-3

The document provides an overview of basic Java syntax including class and method declarations, primitive and non-primitive data types, and variables. A class declaration begins with the class keyword followed by the class name. Methods can be declared within a class and given access specifiers like public. Primitive data types include integers, floats, chars, and more. Non-primitive types are arrays, strings, classes, and interfaces.

Uploaded by

sankalpk261
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Basic Syntax

Class Declaration System output

Create a class with name: Main Create a method with name: demoMethod

A class declaration begins with the "class" keyword. System Method to print
Class
After the class keyword, put the class name.
class Main
System.out.println("Hi, I am Java!");
{
int x = 5;
Standard OutputStream Message to print
(static member)
void printX() {
Class body
System.out.println(this.x);
}
}
System input

Create an object of Scanner class: myObj


Method Declaration Scanner new operator
class
Create a method with name: demoMethod
Access Return type Method Signature Scanner myObj = new Scanner(System.in);
Specifier (Method Name & Parameter List)
Standard InputStream
public int demoMethod (int a, int b)
Object name
{
// Method body
}
Use one of these methods to take input:

nextBoolean() - Reads a boolean value from the user


nextByte()
main() method signature - Reads a byte value from the user

nextDouble() - Reads a double value from the user

Create a method with name: demoMethod nextFloat() - Reads a float value from the user

nextInt() - Reads an int value from the user


Belongs to
the class nextLine() - Reads a String value from the user
Accessible Return type Method name
Everywhere nextLong() - Reads a long value from the user

nextShort() - Reads a short value from the user


public static void main (String[] args)
{
String username = myObj.nextLine();
// Method body
Array of String
} as argument
// Take the whole line as string input

01 Java Cheatsheet
Primitive Data Types

Type Size Range Default Values

byte 8 bits -128 to 127 0


short 16 bits -32,768 to 32,767 0
int 32 bits -2^31 to 2^31 - 1 0
long 64 bits -2^63 to 2^63 - 1 0L
float 32 bits 1.4E-45 to 3.4028235E38 0.0f
double 64 bits 4.9E-324 to 1.797693E308 0.0d
char 16 bits '\u0000' to '\uffff' '\u0000'
boolean 1 bit true or false false

Byte:
Program Stack Memory
byte a = 13;

Memory
0 0 0 0 1 1 0 1
Allocation
a
(name to the
memory location)

Non-Primitive Data Types

Non-Primitive Data Type

Arrays String Class Interface

Java Variables

Memory
int age = 22; Variables in Java are only a name to the
Name to the
memory location where value is stored.
memory location
Data type Variable Name Value age 22

02 Java Cheatsheet

You might also like