Varargs:-: Class Coaching (
Varargs:-: Class Coaching (
In Java 5.0, it adds convenience of using variable arbitrary parameters, known less formally as varargs.
One particularly nice feature about varargs is that a variable-length argument can be take from zero
to n arguments.
class Coaching {
public static void printStudentName(String... names) {
for (String s: names) {// foreach for loop
System.out.println("Hello " + s + ". ");
}
}
public static void main(String[] args) {
printStudentName("Ashok");
printStudentName("Sanjeev", "Shani");
printStudentName("Arun", "Shivani", "Monu", "Sonu");
}
}
At compile time a vararg is converted to an array. The varargs facility is just implicit syntax for creating
and passing arrays, an array can be passed directly.
class Coaching {
public static void printStudentName(String... names) {
for (String s: names) {
System.out.println("Hello " + s + ". ");
}
}
public static void main(String[] args) {
printStudentName(args);
}
}
Rules for Vararg Methods
To declare a method using a var-arg parameter, the var-arg parameter's syntax is three times dot(.) after
parameter’s data type like String... name.
It must be last parameter of method or first if there is only one parameter.
public class Student {
void show(int... i) {
System.out.println("i=" + i);// i[0] to access first element value.
}
}
Method Overload:-
public class Student {
public static void main(String[] arg) {
int k=4;
Student st=new Student();
st.show(k);
}
void show(long lg) {
System.out.println("lg=" + lg);
}
void show(int... i) {
System.out.println("i=" + i);
}
}
Notice:- Widening always beats varags.
class Student{
void test(int... i) {
System.out.println(“student test ");
}
}
Java Operators:-
Assignment Operators
Assignment operator, "=“ is using to initialize a variable.
Compound Assignment Operators:-
(+=, -=, *=, and /=) int k=2; k *= 2 + 5; output:-
Relational Operators:-
(<, <=, >, >=, ==, and !=).
Don't mistake = for == in a boolean expression. The following is legal:
boolean b=false; if(b=true){System.out.println(“true”);} output:-
instanceof Comparison
The instanceof operator is used for object reference variables only, and you can use it to check whether an object is of a particular type.
public static void main(String[] args) {
String s = new String(“Test");
if (s instanceof String) {
System.out.print("s is a String");
}
}
instanceof Compiler Error
You can't use the instanceof operator to test across two different class hierarchies.
For instance, the following will NOT compile:
class Cat { }
class Dog {
public static void main(String [] args) {
Dog d = new Dog();
System.out.println(d instanceof Cat);
}
}
Compilation fails—there's no way d could ever refer to a Cat or a subtype of Cat.
Note:- null instanceOf any class or interface type will always false.
Student[] s=new Student[1];
if(s instanceof Student) output:- false
if(s instanceof Object) output:- true
int[] intArr=new int[]; if(intArr instanceOf Object) output:- true
Arithmetic Operators
We're sure you're familiar with the basic arithmetic operators.
■ + addition
■ – subtraction
■ * multiplication
■ / division
^ exclusive-OR (XOR):-
false +false=false
false +true=true
true +true=false
public class Test{
public static void main(String[] arg) {
Integer i = 42;
String s = (i<40)?"life":(i>50)?"universe":"everything";
System.out.println(s);
}
}
if(args.length == 1 | args[1].equals("test")) {
System.out.println("test case");
} else {
System.out.println("production " + args[0]);
}
}
==, .equals(), compareTo(), and compare()