SAGA
SAGA

Reputation: 349

different Methods call in java

I'm new to java language I confused about different between these 2 methods calls (I don't know exactly it is method call or not)


but if i need to call method to calculate something I call this

salary obj=new salary();

 obj.bonus(45000);

but when we call String length method we call this as

String name="Java lovely";
name.length();

What are the different of these 2 methods .Please help me to overcome this problem.
Thank you

Upvotes: 2

Views: 182

Answers (6)

Rahul Kulhari
Rahul Kulhari

Reputation: 1165

String is a class. String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:

     String str = "Java lovely";

is equivalent to:

     char data[] = {'J', 'a', 'v', 'a', ' ', 'l','o', 'v', 'e','l', 'y'};
     String str = new String(data);

length is a method of String Class.

in upper part that is

      salary obj=new salary();
      obj.bonus(45000);

in this salary is a class and you created object of this 'obj' and bonus is a method that you defined in salary class and you are calling that method

and other is

     String name="Java lovely";
     name.length();

is here same

     String name=new String("Java lovely");

and you are calling length method on String object 'name'.

both are same but one difference is String is constant(immutable) mean once you created it, you can't change it but your salary class is not constant,you can change it.

To understand java better u can read this book. Head first Java

Upvotes: 1

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

 salary obj=new salary(); // You are initializing salary class

 obj.bonus(45000); // Your salary class contains bonus method and it takes
                      int argument

So you are call bonus method and parse 45000 to that.

 String name="Java lovely";  // You are define a String
 name.length(); // this will return the length of name String, no need input 
                argument and return int length 

Upvotes: 0

T_01
T_01

Reputation: 1359

What do you men by difference? That the first call has something between the () ? And name.length not? That is because the method bonus is an method with parameters. It expects an value, at this case an integer or int to do something with this. but Name.length doesnt mean any values, it just returns the saved length of the string.

Or where else you see a difference?

obj.bonus(number); and
name.length (); 

is actually the same...

Sry if this wasnt your question.

Edit: or do you mean the constructor? This is because string is special in java, actually new String("lalala"); is right.

Upvotes: 0

Muhammed Refaat
Muhammed Refaat

Reputation: 9103

it's the same, as String is a ready included java class. you can make :

String name = new String("dsdsd");

it seems like

String name = new String();
name = "dsdsd";
name.length();

Upvotes: 0

Ugur Artun
Ugur Artun

Reputation: 1804

It is exactly same. Both obj and name are instance of their class. BUt String is exceptional class in java.. It can be create like above without new keyword.

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

String name="Java lovely";

You might confused with the string literal.

String is special in java

You will clear if I wrote

String s = new String("Java Lovely");
s.lenght();

Upvotes: 3

Related Questions