- Ternary Operator
condition ? ifConditionTrueWhatShouldHappen : ifConditionFalseWhatShouldHappen
This can be use instead of ,Normal if and else .- Substring
- public String substring(int startIndex) :
This method returns new String object containing the substring
of the given string from specified startIndex (inclusive).
- public String substring(int startIndex, int endIndex):
This method returns new String object containing the substring of the
given string from specified startIndex to endIndex (exclusive).
Index starts from 0 .Returns index where found, or -1 if not found
ex :-
String text = "Here there everywhere";
int a =text.indexOf("there"); // a is 5
int b =text.indexOf("er"); // b is 1
int c =text.indexOf("eR"); // c is -1, "eR" is not found
- For Checking Null
will use short-circuit evaluation, meaning it ends if the first condition of aif(foo != null && foo.bar()) { someStuff(); }logical ANDis false. - The
&&and||operators "short-circuit", meaning they don't evaluate the right hand side if it isn't necessary. The&and|operators, when used as logical operators, always evaluate both sides.
- Variable Arguments (Varargs) in Java
- There can be only one variable argument in a method.
- Variable argument (varargs) must be the last argument.
- ex:-
// Java program to demonstrate varargsclassTest1{// A method that takes variable number of intger// arguments.staticvoidfun(int...a){System.out.println("Number of arguments: "+ a.length);// using for each loop to display contents of afor(inti: a)System.out.print(i +" ");System.out.println();}// Driver codepublicstaticvoidmain(String args[]){// Calling the varargs method with different number// of parametersfun(100);// one parameterfun(1,2,3,4);// four parametersfun();// no parameter}}