2

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

SAGA
  • 349
  • 5
  • 15

6 Answers6

3
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();
Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

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

Rahul Kulhari
  • 1,115
  • 1
  • 15
  • 44
0

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.

Ugur Artun
  • 1,744
  • 2
  • 23
  • 41
0

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();
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
0

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.

T_01
  • 1,256
  • 3
  • 16
  • 35
  • 1
    According to my knowledge in 1st code it will pass to bonus method in side () but in 2nd method it will pass to it using .(dot) – SAGA Aug 06 '13 at 07:18
  • Yes.. that could be an corrwct explanation :) you can also say: the name.length method doesnt do anything, it is handled as an number. You can do this: int a = name.length(); but obj.bonus seems to do something with this number, and cannot rwtuen anything. The return type is void. (=nothing) – T_01 Aug 06 '13 at 07:25
0
 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 
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115