String str =String.valueOf(value);int i =Integer.parseInt(str);double d =Double.parseDouble(str);int i = (int) numeric expression;
Blocks
C++ if, else if, else, while, do while, for, switch
for ( var : collection ) {//statements}
Data Structures
Builtin Array:
[] syntax, fixed size, store obj & primitatives(int), .length
ArrayList:
only store objects(Integers), access with functions(.get())
dynamic size, .size(), generics
importjava.util.*;//allimportjava.util.ArrayList;ArrayList<String> arr =newArrayList<String>();for (int i =0; i <arr.size(); i++) {System.out.println("Index "+ i +" : "+ arr[i]); }importjava.util.HashMap; HashMap<String,Integer> map =newHashMap<>(); Set<String> hash_Set =newHashSet<String>(); //HashSet, LInkedHashSet, or TreeSet
.add(item)
.get(i)
.size()
.remove(i)
.set(i, val)
Object Methods
Equals
Default will check object reference is equal, can override equal to be different, essential for Java data structures like HashMaps which will use this equals
Comparator & Comparable
importjava.io.*; importjava.util.*; classMovieimplementsComparable<Movie> { privateString name; privateint year; publicintcompareTo(Movie m) { returnthis.year-m.year; } publicMovie(String nm,int yr) { this.name= nm; this.year= yr; } publicStringgetName() { return name; } publicintgetYear() { return year; } } // Class to compare Movies by name classNameCompareimplementsComparator<Movie> { publicintcompare(Movie m1,Movie m2) { returnm1.getName().compareTo(m2.getName()); } } classMain{ publicstaticvoidmain(String[] args) { ArrayList<Movie> list =newArrayList<Movie>(); //add elementsNameCompare nameCompare =newNameCompare(); Collections.sort(list, nameCompare); //by nameCollections.sort(list); // by default year } }