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

Java Syntax Cheatsheet

This document is a Java Basic Syntax Cheatsheet that provides essential syntax and operations for various Java programming tasks, including setup, input handling, character manipulation, function declaration, string operations, array management, and ArrayList usage. It includes examples for importing libraries, reading different data types, and manipulating strings and arrays. The cheatsheet serves as a quick reference for common Java programming syntax and operations.

Uploaded by

shreshthij49
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)
124 views2 pages

Java Syntax Cheatsheet

This document is a Java Basic Syntax Cheatsheet that provides essential syntax and operations for various Java programming tasks, including setup, input handling, character manipulation, function declaration, string operations, array management, and ArrayList usage. It includes examples for importing libraries, reading different data types, and manipulating strings and arrays. The cheatsheet serves as a quick reference for common Java programming syntax and operations.

Uploaded by

shreshthij49
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

Java Basic Syntax Cheatsheet

Category Operation Syntax / Example

Setup Import Scanner import java.util.Scanner;


Import ArrayList import java.util.ArrayList;
Import Collections import java.util.Collections;
Create Scanner Scanner sc = new Scanner(System.in);
Input Read String String s = sc.nextLine();
Read Integer int x = sc.nextInt();
Read Boolean boolean b = sc.nextBoolean();
Read Char (1st of input) char c = sc.next().charAt(0);
Char Handling To Lowercase (char) Character.toLowerCase(c);
To Uppercase (char) Character.toUpperCase(c);
Function Declare Function public static returnType name(params) { ...
Example Void Function public static void sayHi() { System.out.print
Strings To Lowercase s.toLowerCase();
To Uppercase s.toUpperCase();
Char at Index s.charAt(index);
String Length s.length();
Substring s.substring(start, end);
Equals s.equals(t); / s.equalsIgnoreCase(t);
Contains Substring s.contains("abc");
Index of Substring s.indexOf("abc");
Replace Characters s.replace("a", "b");
String to Int int x = Integer.parseInt(s);
Int to String String s = Integer.toString(x);
String to Char Array char[] arr = s.toCharArray();
Arrays Declare Array int[] arr = new int[size];
Read Array for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
Print Array for(int x : arr) System.out.print(x + " ");
Array Length arr.length;
2D Arrays Declare 2D Array int[][] arr = new int[rows][cols];
Input 2D Array for(int i=0;i<r;i++) for(int j=0;j<c;j++) arr[i][j]
Print 2D Array for(int[] row : arr) for(int val : row) System.o
ArrayList Declare ArrayList ArrayList<Integer> list = new ArrayList<>()
Add Element list.add(x);
Get Element list.get(index);
Set Element list.set(index, value);
Remove by Index/Object list.remove(index); / list.remove((Integer) v
Size list.size();
Check Empty list.isEmpty();
Loop through ArrayList for(int x : list) System.out.println(x);
Sort ArrayList Collections.sort(list);

You might also like