Differentiate Between & and && in Java Programming: If They Use As Logical AND
Differentiate Between & and && in Java Programming: If They Use As Logical AND
com
(x != 0) & (1/x > 1) <-- this means evaluate (x != 0) then evaluate (1/x >
1) then do the &. the problem is that for x=0 this will throw an exception.
(x != 0) && (1/x > 1) <-- this means evaluate (x != 0) and only if this is
true then evaluate (1/x > 1) so if you have x=0 then this is perfectly safe
and won't throw any exception if (x != 0) evaluates to false the whole thing
directly evaluates to false without evaluating the (1/x > 1).
For integer arguments, the single ampersand ("&")is the "bit-wise AND"
operator. The double ampersand ("&&") is not defined for anything but two
boolean arguments.
For all other argument types and combinations, a compile-time error should
occur.
}
If using &:
String str = null;
if(str!=null & !str.equals("")){ // the right expression will execute, and
throw the NullPointerException
}
An other more example:
int x = 0;
int y = 2;
if(x==0 & ++y>2){
System.out.print(“y=”+y); // print is: y=3
}
int x = 0;
int y = 2;
if(x==0 && ++y>2){
System.out.print(“y=”+y); // print is: y=2
}
& can be used as bit operator
& can be used as Bitwise AND operator, && can not.
The bitwise AND " &" operator produces 1 if and only if both of the bits in its
operands are 1. However, if both of the bits are 0 or both of the bits are
different then this operator produces 0. To be more precise bitwise AND " &"
operator returns 1 if any of the two bits is 1 and it returns 0 if any of the bits
is 0.
Object
It is a basic unit of Object Oriented Programming and represents the real life
entities. A typical Java program creates many objects, which as you know,
interact by invoking methods. An object consists of :
1. State : It is represented by attributes of an object. It also reflects the
properties of an object.
2. Behavior : It is represented by methods of an object. It also reflects
the response of an object with other objects.
3. Identity : It gives a unique name to an object and enables one object
to interact with other objects.
As we declare variables like (type name;). This notifies the compiler that we
will use name to refer to data whose type is type. With a primitive variable,
this declaration also reserves the proper amount of memory for the variable.
So for reference variable, type must be strictly a concrete class name. In
general, we can’t create objects of an abstract class or an interface.
Dog tuffy;
The properties object contains key and value pair both as a string. The
java.util.Properties class is the subclass of Hashtable.
It can be used to get property value based on the property key. The
Properties class provides methods to get data from the properties file and
store data into the properties file. Moreover, it can be used to get the
properties of a system.
Method Description
public void load(Reader r) It loads data from the Reader
object.
To get information from the properties file, create the properties file first.
db.properties
1. user=system
2. password=oracle
Methods in Java
Inheritance in Java
1. Inheritance
2. Types of Inheritance
3. Why multiple inheritance is not possible in Java in case of class?
The idea behind inheritance in Java is that you can create new classes that
are built upon existing classes. When you inherit from an existing class, you
can reuse methods and fields of the parent class. Moreover, you can add
new methods and fields in your current class also.
The extends keyword indicates that you are making a new class that
derives from an existing class. The meaning of "extends" is to increase the
functionality.
1. class Employee{
2. float salary=40000;
3. }
4. class Programmer extends Employee{
5. int bonus=10000;
6. public static void main(String args[]){
7. Programmer p=new Programmer();
8. System.out.println("Programmer salary is:"+p.salary);
9. System.out.println("Bonus of Programmer is:"+p.bonus);
10. }
11. }
On the basis of class, there can be three types of inheritance in java: single,
multilevel and hierarchical.
In java programming, multiple and hybrid inheritance is supported through
interface only. We will learn about interfaces later.
Encapsulation in Java
We can create a fully encapsulated class in Java by making all the data
members of the class private. Now we can use setter and getter methods to
set and get the data in it.
By providing only a setter or getter method, you can make the class read-
only or write-only. In other words, you can skip the getter or setter
methods.
It provides you the control over the data. Suppose you want to set the
value of id which should be greater than 100 only, you can write the logic
inside the setter method. You can write the logic not to store the negative
numbers in the setter methods.
It is a way to achieve data hiding in Java because other class will not be
able to access the data through the private data members.
The encapsulate class is easy to test. So, it is better for unit testing.
The standard IDE's are providing the facility to generate the getters and
setters. So, it is easy and fast to create an encapsulated class in Java.
Let's see the simple example of encapsulation that has only one field with its
setter and getter methods.
File: Student.java
Abstraction in Java
Another way, it shows only essential things to the user and hides the
internal details, for example, sending SMS where you type the text and send
the message. You don't know the internal processing about the message
delivery.
Abstraction lets you focus on what the object does instead of how it does it.
2. Interface (100%)
Points to Remember
o It cannot be instantiated.
o It can have final methods which will force the subclass not to change
the body of the method.
In this example, Bike is an abstract class that contains only one abstract
method run. Its implementation is provided by the Honda class.
Polymorphism in Java
Upcasting
If the reference variable of Parent class refers to the object of Child class, it
is known as upcasting. For example:
1. class A{}
2. class B extends A{}
1. A a=new B();//upcasting
For upcasting, we can use the reference variable of class type or an interface
type. For Example:
1. interface I{}
2. class A{}
3. class B extends A implements I{}
B IS-A A
B IS-A I
B IS-A Object
Since Object is the root class of all classes in Java, so we can write B IS-A
Object.
In this example, we are creating two classes Bike and Splendor. Splendor
class extends Bike class and overrides its run() method. We are calling the
run method by the reference variable of Parent class. Since it refers to the
subclass object and subclass method overrides the Parent class method, the
subclass method is invoked at runtime.
Since method invocation is determined by the JVM not compiler, it is known
as runtime polymorphism.
1. class Bike{
2. void run(){System.out.println("running");}
3. }
4. class Splendor extends Bike{
5. void run(){System.out.println("running safely with 60km");}
6.
7. public static void main(String args[]){
8. Bike b = new Splendor();//upcasting
9. b.run();
10. }
11. }
Test it Now
Output:
Consider a scenario where Bank is a class that provides a method to get the
rate of interest. However, the rate of interest may differ according to banks.
For example, SBI, ICICI, and AXIS banks are providing 8.4%, 7.3%, and
9.7% rate of interest.
Note: This example is also given in method overriding but there was no
upcasting.
1. class Bank{
2. float getRateOfInterest(){return 0;}
3. }
4. class SBI extends Bank{
5. float getRateOfInterest(){return 8.4f;}
6. }
7. class ICICI extends Bank{
8. float getRateOfInterest(){return 7.3f;}
9. }
10. class AXIS extends Bank{
11. float getRateOfInterest(){return 9.7f;}
12. }
13. class TestPolymorphism{
14. public static void main(String args[]){
15. Bank b;
16. b=new SBI();
17. System.out.println("SBI Rate of Interest: "+b.getRateOfInterest());
18. b=new ICICI();
19. System.out.println("ICICI Rate of Interest: "+b.getRateOfInterest());
20. b=new AXIS();
21. System.out.println("AXIS Rate of Interest: "+b.getRateOfInterest());
22. }
23. }
Test it Now
Output: