Java Tutorial – 2 | Edureka

Java Tutorial – 2 | Edureka

FavoriteLoadingAdd to favorites

Take instructor-led Live class on Java Tutorial at :

Java468x60

Constructor:
A constructor is used in the creation of an object.
In the previous we created a class and different things with in a class like variables, methods. In order to use the variables and methods of the class we need to create an object.
You can consider class as blue print and using that blue print you create many kinds of objects.
How the objects are created using their blueprint?
It is created using constructor.
As the name suggests it actually constructs the objects according to the class.
A constructor is a special method that has no return type. When you declare a method it should have a return type either it should be an integer, float or any data type depending on the logic of the program. You use void for method which does not return anything.
But constructors do not have any return type as a normal method. So constructor is a special method with no return type.
Must have same name as the class it is in.
E.g.: If you have a class with name “Class1” then constructor should also be named as “Class1”.
Constructors can have access specifiers. If use public then that constructor will be accessed publically.
When a variable is declared without any access specifier then it can be taken as “default” which means that the classes present in the same package can be access that variable.

Constructor Overloading:
When we have two or more constructors with same name and different type of arguments then we say that we are overloading the constructor. This is what we call as constructor overloading.
If we have different names overloading does not happen.
Hence overloading of a constructor takes place if and only if:
All constructors defined for a class should have same name.
They should have different type of arguments.
They might also differ in the number of arguments and order of arguments passed.
Constructor overloading gets executed at run-time.
When we create we use “new” operator because it helps in allocating memory for the object.
What happen if I have two constructors with same name and with no parameters?- it returns an error
When we create an object of a class, you can use only one constructor of that class when constructor overloading is present in class.
If you want to use the other constructor then we have create a different object of that class and use it.
Hence you can use only one constructor of the class having many constructors for an object of that class.
Do we need to initialize all the instance variables of the class in the constructor? –No. If some of the instance variables are not initialized in the constructor then those variables will be assigned default values.
Java Syntax for Loop Constructs:
Loop means that it will do the things again and again. So when you want to do something repeatedly we use loops. There are different types of loop.
For loop
While loop
Do while loop
For each loop
For Loop:
It allows the code to be executed repeatedly.
This loop is typically used when the number of iterations is known.
Iteration means the number of times the loop should be executed.
For Loop working:
“for” is k
This repeats until the condition becomes false.
The difference between do-while loop and while loop is:
In while loop first the condition is evaluated and if condition is true then only the statements in the while loop will execute. If the condition is false then the statements in while loop will not execute.
But if you consider the reverse is happening. i.e first the statement in the loop will be executed and then condition is evaluated. Even if the condition is true or false the statements are getting executed at least once.
In general in a group of nested loops, which loop is executed the most number of times?-the innermost loop.

Please write back to us at support@edureka.in or call us at +91-8880862004 for more information.

Visit –

source

By |2020-02-02T01:31:50+00:00February 2nd, 2020|Java Video Tutorials|9 Comments

9 Comments

  1. Jay Yadav February 2, 2020 at 1:31 am - Reply

    Please help me with this Assignment Solution?

    Java Basics: Michael and Bruce, Who wins the race ?

    Michael and Bruce are friends. Both of them liked the same girl.
    One day they started fighting over her and decided that both of them
    will write a number on a chit. The one whose number will be smaller
    will forget the girl and get out of other’s way.
    The problem was that they were not educated and hence they are still
    waiting for someone, who could help them to implement the plan.
    Now try to implement a program which can help them, using the above
    strategy.

  2. ThatsRight February 2, 2020 at 1:31 am - Reply

    Kindly edit the audio and give some bass. It is pinching me to my ears..

  3. Shubham Jaiswal February 2, 2020 at 1:31 am - Reply

    First of all your  whole initiative is really appreciating.I would like to mention that please update the full series of any program that would be more beneficial for us.

  4. sanjukta ghosh February 2, 2020 at 1:31 am - Reply

    very gud tutorial…..thanks sir…

  5. lipi February 2, 2020 at 1:31 am - Reply

    Very good tution.Just a very annoying voice ,needs a lot of patience to listen.But clears a lot of doubts.very helpful.

  6. Dorah Lwanzo February 2, 2020 at 1:31 am - Reply

    Thanks for the tutorial, it was really helpful
    I can't wait to watch

  7. Rashmi Sethy February 2, 2020 at 1:31 am - Reply

    superb tutorial….

  8. Vikash Sharma February 2, 2020 at 1:31 am - Reply

    can ew write like
    int p=1;
    while(while(while….(while  p>1))))))))……)))))
     
    {
                 statement 1;
                  stasement 2;

     }             

    give an answer and on chocolet will be freeee 😀 😀

  9. Alish Bhaskar February 2, 2020 at 1:31 am - Reply

    Awesome edureka..

Leave A Comment

*