1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

String in java

  1. Jul 7, 2011 #1
    "String" in java..

    1. The problem statement, all variables and given/known data
    its a bit conceptual question.
    there is a string class which we use to store strings of letters.
    1) but we don't create a reference to it,nor we instantiate it.
    2) not even a method is invoked mean in printing, we just mention name of string class.
    3) also we even can declare its type-public/private/default/protected.

    its just used in a manner of a primitive data type...
    can someone please enlighten me on this topic...?

    thanks.


    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. Jul 7, 2011 #2
    Re: "String" in java..

    same problem is troubling me with java.util package..
    in a class we declare it lie .
    public/private Vector/ArrayList<> xyz.....
    .
    .
    how can we define a type of predefined class.?
     
  4. Jul 7, 2011 #3
    Re: "String" in java..

    So what exactly confuses you about the String class? I guess I don't quite understand the question you have. Maybe someone else can enlighten me as well.
     
  5. Jul 7, 2011 #4
    Re: "String" in java..

    Sure you do. All objects are references in Java, including String. And you do instantiate it, though not necessarily with new. These two lines are functionally the same:

    String str = "Blah";

    and

    String str = new String("Blah");

    A toString() method is invoked implicitly. Every class has a toString method, even if it doesn't implement it. The base Object class contains a default toString. It usually doesn't print anything super useful unless you override it with a toString more specific to that class, but it's always there. So these two lines are also functionally the same:

    System.out.println(str);

    and

    System.out.println(str.toString());

    Obviously this is super silly when the println method actually takes a String as a parameter, and you're passing it a String already, but it's just an example. Even the String class has a toString() method.

    As with any class member. There's nothing exceptional about that, so I'm not sure what the problem is here.

    I wouldn't complain, it's easier to work with when you can sort of treat it like a primitive. I do mean "sort of", though. It remains a class and it's not quite the same as a primitive type. For one, it has methods. For another, a primitive type can't be null.

    I'm not sure what you're asking here. If you want to have a member variable in your class for, for example, a list of String objects, you just do this:

    Code (Text):

    import java.util.ArrayList;

    public class SomeTest {
        public ArrayList<String> stringList;

        public SomeTest() {
            stringList = new ArrayList<String>();
        }
    };
     
    But I'm not sure what problems you're having with it, so if that doesn't explain it for you, perhaps you can clarify.
     
  6. Jul 8, 2011 #5
    Re: "String" in java..

    you declare
    public ArrayList<String> ....

    the thing is you declare its type public, you can even declare it private,protected or default.
    since its already a defined class java.util.ArrayList..... its type should have been already 'public' so that it can be accessed and referenced by other classes using an import statement....
    but we can even declare it private...........

    this thing is fussing in my mind....
     
  7. Jul 8, 2011 #6

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Re: "String" in java..

    Ah, you have Java grammar wrong.

    In public ArrayList<String> stringList, the word public is a modifier for stringList -- it's declaring stringList to be public.
     
  8. Jul 8, 2011 #7
    Re: "String" in java..

    okay.... its the reference StringList that i am declaring public.....
    wat a silly qn have i asked :mad:
     
  9. Jul 14, 2011 #8
    Re: "String" in java..

    The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuffer class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: String in java
  1. HeapSort Java (Replies: 3)

Loading...