you can use + operator on datatypes smaller than int otherwise anything that gets promoted to Integer.
To get max and min value of data type :
int high =Integer.MAX_VALUE;int low =Integer.MIN_VALUE;
Strings
Important functions
equalsIgnoreCase
toLowerCase
To check Strings in Java don't use "==" instead use :
str1.equals(str2)
charAt(0)
String.contains()
s.indexOf('i')
split(";")
join()
toString() Dont use with strings
String is a final and immutable class
To create a mutable string in java, Java has two classes
StringBuffer and
StringBuilder
String s ="this is an example";String a =s.substring(11); // a will hold the string starting at character 11 // until the end ("example")
String s ="popcorn";System.out.println(s.replace('p','W'));
String Builder
StringBuilder sb =newStringBuilder();sb.append("One=").append(one).append(", Color=red")System.out.print(sb);// Prints "One=1, Colour=red" // followed by an ASCII newline.String result =sb.toString();
Formatter
int one =1;String color ="red";Formatter f =newFormatter();System.out.print(f.format("One=%d, colour=%s%n", one, color));// Prints "One=1, Colour=red" // followed by the platform's line separator
String Joiner
StringJoiner sj =newStringJoiner(", ","[","]");for (String s :newString[]{"A","B","C"}) {sj.add(s);}System.out.println(sj);// Prints "[A, B, C]"
publicclassInternExample{ publicstaticvoidmain(String args[]){ String s1=newString("hello"); String s2="hello"; String s3=s1.intern();// returns string from pool, // now it will be same as s2 System.out.println(s1==s2);// false because reference variables are // pointing to different instance System.out.println(s2==s3);// true because reference variables are // pointing to same instance }}
BigDecimal a =newBigDecimal("42.23");BigDecimal b =newBigDecimal("10.001");a.add(b); // a will still be 42.23BigDecimal c =a.add(b); // c will be 52.231
Creating Big Decimals
BigDecimal a =newBigDecimal(5);
Comparing
a.compareTo(newBigDecimal(0));// a is greater, returns 1a.compareTo(newBigDecimal(5));// a is equal, returns 0a.compareTo(newBigDecimal(10));// a is less, returns -1
BigDecimal a =newBigDecimal("5");BigDecimal b =newBigDecimal("7");//Equivalent to result = a + bBigDecimal result =a.add(b);//Equivalent to result = a - bBigDecimal result =a.subtract(b);//Equivalent to result = a * bBigDecimal result =a.multiply(b);//Equivalent to result = a / bBigDecimal result =a.divide(b);//Equivalent to result = a % bBigDecimal result =a.remainder(b);//Equivalent to result = a^10BigDecimal result =a.pow(10);//Equivalent to result = MAX(a,b)BigDecimal result =a.max(b);//Equivalent to result = MIN(a,b)BigDecimal result =a.min(b);BigDecimal a =newBigDecimal("5234.49843776");// Moves the decimal point to 2 places left of // current positionBigDecimal result =a.movePointLeft(2);// Result : 52.3449843776BigDecimal a =newBigDecimal("5234.49843776");// Moves the decimal point to 3 places right of // current positionBigDecimal result =a.movePointRight(3);System.out.println(result);// Result : 5234498.43776
BigInteger value1 =newBigInteger("10");BigInteger value2 =newBigInteger("10");BigInteger sum =value1.add(value2);BigInteger sub =value1.subtract(value2);BigInteger div =value1.divide(value2);BigInteger mul =value1.multiply(value2);BigInteger value1 =newBigInteger("10");BigInteger power =value1.pow(3);BigInteger power =value1.remainder(value2);System.out.println(value1.gcd(value2));System.out.println(value1.max(value2));System.out.println(value1.min(value2));
Comparing
BigInteger one =BigInteger.valueOf(1);BigInteger two =BigInteger.valueOf(2);if(one.equals(two)){System.out.println("Equal");}else{System.out.println("Not Equal");}
In general, do not use use the == operator to compare Big Integers
== operator: compares references; i.e. whether two values refer to the same object
equals() method: compares the content of two Big Integers.
BigInteger reallyBig =BigInteger.valueOf(10);BigInteger reallyBig1 =BigInteger.valueOf(100);if(reallyBig.compareTo(reallyBig1) ==0){//code when both are equal.}