1
public class UpLower {    
    public static void main(String[] args) {  
        String str = "HOW ARE YOU";  
        String upper_str = str.toLowerCase();  
        System.out.println("Original String: " + str);  
        System.out.println("String in uppercase: " + upper_str);  
    }  
}

This program converts the string from upper to lower character. I can't understand this program.

My questions is

method toLowerCase() is in String class which is in lang package.In java, we need to create an object for the class to access the non-static methods of that class. If that is the case, without creating object for the class String how can we directly access the method toLowerCase().

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
Selvaperumal
  • 667
  • 5
  • 11
  • What does it mean to access a method without an object? How do you envision this working? What will it do? – Mad Physicist Jan 19 '18 at 07:14
  • Are you a Python guy? – Mad Physicist Jan 19 '18 at 07:15
  • Are you envisioning this as passing a value to `toLowerCase(String s)`? – Pankaj Gadge Jan 19 '18 at 07:15
  • You could always create a 'new String("")' in place and directly call a method on that object, like 'new String("").toLowerCase()'. That kind of saves you a few lines. But I am not familiar with any possibility to do this directly with only a literal – Sossenbinder Jan 19 '18 at 07:16
  • yes i envision it like toLowerCase(String s) only. but how the above program works. – Selvaperumal Jan 19 '18 at 07:16
  • @Mad I hope you are talking about programming language.. :) In java you need to create the object to access a nonstatic methods.. – ZaoTaoBao Jan 19 '18 at 07:16
  • 3
    `upper_str = str.toLowerCase();` dayum dat logic. for something like `toLowerCase(String s)` you have to create a util class with a `public static String toLowerCase(String s)` and have it `return s.toLowerCase()` – XtremeBaumer Jan 19 '18 at 07:19
  • @ZaoTaoBao. Of course, this looks like the sort of thing someone used to Python would try to pull off. – Mad Physicist Jan 19 '18 at 07:19
  • You can't. Period. Java is an OO language. It uses methods. Not global functions. So instead of toLowerCase(string), you use string.toLowerCase(). – JB Nizet Jan 19 '18 at 07:19
  • 2
    You can do `"HOW ARE YOU".toLowerCase();`. The problem is that you would lose the original string. – Mad Physicist Jan 19 '18 at 07:20
  • Do you just want to get rid of the second string(upper_str)? – Julius Hörger Jan 19 '18 at 07:26
  • In line, String str = "HOW ARE YOU"; you are creating string object only named str. so that, you are able to access the method of it. In java, string class initialization is different from any other object initialization using new operator. – Murthi Jan 19 '18 at 07:30

4 Answers4

2

You are right about your understanding of static methods and instance methods.

toLowerCase() is infact an instance method of object which are of type String.

But in java String has a special handling. When you write String str = "How Are You" str will be having reference to String object. So actually you are calling instance method toLowerCase() on the returned object only.

For starter you can think that String str = "How Are You"; is similar to String str = new String("How Are You");.

But actually there are subtle differences in the two statements String s = "something" and String s = new String("something");

If you want to know reason, try reading about String pool and String immutability in Java.

Pranav Maniar
  • 1,545
  • 10
  • 22
1

No you cannot. Inorder to access any objects method, you need to create an instance of it.

If you want to copy the same functionality, you can have a look at the source code of it and create another function. But I don't think it makes sense, if you are trying to lowercase a String, that's itself a string object and you can call lowerCase on it. ehm ?

And you are confused by String initialisation, give a read How can a string be initialized using " "?

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

String is special. When you did: String str = "HOW ARE YOU";, a new string object got created. Refer http://net-informations.com/java/cjava/create.htm

gargkshitiz
  • 2,130
  • 17
  • 19
  • The Javadoc of the [String](https://docs.oracle.com/javase/9/docs/api/java/lang/String.html) class mention `The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.`. – SubOptimal Jan 19 '18 at 08:01
-1

String s1 = "Foo"; // initialize an object in Heap if "Foo" is not in Heap. String s2 = "Foo"; // first it check if the object exists in heap then it will return its reference, other wise it initialize new object with this value. so, when you first write String s1 = "Foo", internally it creates and initialize an String objects, that's why its allowing you to access its methods.