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