Posts Tagged java

Java Tutorial 12 – Constructors

constructors are a special type of method which we can use to initialize Objects when we create it. If u do not use constructor, objects contains all the variables and methods, But Using constructor you can initialize values of variables. For example you can initialize some variables to zero or Strings to default values and etc. Lets Clear this with an example. Create two classes Tom and Jerry.

Tom:

public class Tom {

public static void main(String[] args){
Jerry object = new Jerry();
object.print();
}
}

Jerry:

public class Jerry {
int a;
String b;

public void print(){
System.out.println(“int a = “+a);
System.out.println(“String b = “+b);
}
}

Now if u run the Tom.java you will see the output like:

int a = 0
String b = null

Now if you want to initialize String b to some text say “Hi” and int a to value 5 whenever the new object is created, you can do it by creating constructor. There are some rules for it:

  • Name of the constructor must be same as name of class.
  • there is no other rule 😛

Now lets create constructor for Jerry class and initialize variables.

Jerry:

public class Jerry {
int a;
String b;

Jerry(){
a=5;
b = “Hi”;
}
public void print(){
System.out.println(“int a = “+a);
System.out.println(“String b = “+b);
}
}

 

!!!!! yap writing return type and access modifiers is optional. We can define Constructor by just Jerry(){ … }.

Now if you run the Tom.java you will see the output like:

int a = 5
String b = Hi

Here constructor initialized both variables when we created an object of Jerry.

catch you again later…

doubts are invited.

have a nice day.

Peace.

, , , , , ,

Leave a comment

Java Tutorial 5 – Creating methods

We have used system defined methods like println() and nextInt(), Now let’s create our own method and use it.

Defining method is very useful when bunch of code is getting repeated,  just put that code in a method and just call that method instead of writing all the code again

create a class and add this code as always we do.

  1. Why int and Scanner before main method? 

    if we create int inside the main method, then, only main method can use or change that int. so defining anything in the class and outside any method is called Global declaration.

  2. Why Static?
    to use that int into the main or any other method because main itself is static so any variable used within it must be static and thats why we also crated all the methods static.

We created 3 methods and it can be defined anywhere in the class.Observe the out put and you will dig out everything.

u can create different methods of adding subtracting etc. and create  calc.

Doubts invited,

Have a nice day, Peace.

, , , , , , ,

Leave a comment

Java Tutorial 3 – If Else Statements

If else is exactly as same as If else in C/C++.

Lets do this in New Class to create a new Class don’t create new project. click on package above your class and select new –> java class and give name to the class anything you want then hit enter.

Add this code to new created class and you might get idea.

  1. create main() method.
  2. defined an int and a Scanner object for user input (refer to previous tutorial)

Here i have not used {} after if() because if there is only 1 line in the if(){ … } it is optional to do {}.

you can try it by removing {} from else also.

You can add more den 1 condition in if statement by separating them with &&(and)  ||(or)

else if() is also the same and u can try it at your own.

(what the fridge ! is it Home work !)

Again Doubts are invited.
Have a nice 1, Peace.

, , , , , ,

Leave a comment

Java Tutorial 2 – User Input and Maths

First add this code:

Java is case sensitive so take care while typing.

How it works:

to get input from user we need to create a Scanner object that takes input.

We created Scanner object named “input”, you ca name it anything you like (eg. popcorn, bitch).

Now we created 3 doubles “x , y and ans”. Double is simply a variable that hold numeric value with decimal point.

We added System.out.println() lines to make it interactive.

in line “x= input.nextDouble();” we used input object and it’s “nextDouble()” method which returns a double and we stored it in the x.

We did same for variable y.

You can see suggestions while typing so you can find out other methods like nextInt() etc…

now understand this, System.out.println(“x+y =” + (x+y));

here “x+y =” is just a string but (x+y) is a value.

( confused..)

Any string in Java must be written in “…” and more than one strings can be printed together as “String 1” + “String 2”

and if u want to print string, int, double together simply saparet them with +.

for eg. If u want to print “your age is 10” and 10 is stored in variable x, you can do this by

System.out.println(“your age is ” + x ) and it will print the same.

And ya if you run it, it will run your previous class, but to run this class click shift+F6.

Thats it for this tutorial. Doubts are welcomed.

have a good 1, Peace.

, , , , , , , , ,

Leave a comment

Java Tutorials

Hi guys im starting Java Basuc tutorials here to give you kick-start with Java.

I’ll start it within few days so keep checking.

In case of any doubt or request u can comment here, i’ll ans soon as possible.

, ,

1 Comment

Design a site like this with WordPress.com
Get started