Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

[Java]How can we check java class profile ?

Yes, we can quickly check Java Class profile with our terminal.
On daily basis development it is easy to check class profile like what are the methods and constructors are declared/implemented inside a class.

Let's check:

Open your terminal(ctrl+alt+t) if it is Ubuntu machine.

[Java] If we print the object reference what will be the output ?


Initially, when I'm trying to learn JAVA concepts quiet confusing for me.
If I print MyClass object reference it will print the hashcode with prefix "@".

      - Yes, It's fair enough!. 


             MyClass objectRef  = new MyClass(); 
                       System.out.println(objectRef); 

Output: @[hashcode]

But when I print the String or Collection object reference it's printing the string content and collection Object elements respectively.
   - Isn't it confusing ?

[Java]What is the Object and Object reference ?


  • In Java, all objects are accessed by reference, and you never have direct access to the object itself.
  • Reference :- is a variable that has a name and can be used to access the contents of an object, A reference can be assigned to another reference passed to a method, or returned from a method.

    default value of object reference is  "null";


                 MyClass object ; 
  • Object:- is an entity that's exists in memory allocated by the Java run time environment, An object sits on the heap memory when we create an object like

Can we call static members of class directly ?


   - Yes if it is same Class member.

If Class has static/class member(Static member also known as class members) we can access in following ways:

  1. Directly
         If your trying to access static member with in same class you can call it off with out any reference.

  2. With Class name
       If static member is not present in same class  We must access with with Class name
  3. With Object or Object reference

[JAVA] How "System.out.println()" works?

Here your going to understand how SOP() works.  
For understanding this we need to recall few basics of Java:

  • dot (.) operator in Java: In Java . (dot operator) is used only to call methods or variables. So we can say out is either method or variable.
  • Methods in Java : we know methods always have parenthesis ‘( )’ after method name, So out cannot be a method in Java. So out its a variable and println() is a method.
  • Class name in Java: Class name should start with Capital letter ideally in Java, So System is a class.
Now with basic knowledge of Java we know :

  • System is a Class
  • out is a Variable
  • println() is a method
Lets get more in details:
out variable: static or instance?