Comp Sci How Does Java Treat Strings Like Primitive Data Types?

  • Thread starter Thread starter the_kool_guy
  • Start date Start date
  • Tags Tags
    Java Strings
AI Thread Summary
Java treats strings as objects of the String class, which can be used similarly to primitive data types. Strings are instantiated implicitly when declared with a literal, and while they don't require explicit reference creation, they still invoke methods like toString() when printed. The visibility of string variables can be modified using access modifiers like public or private, similar to other class members. Although strings behave like primitives in some contexts, they remain objects with methods and can be null, distinguishing them from true primitive types. Understanding these nuances helps clarify how Java manages string data.
the_kool_guy
Messages
37
Reaction score
0
"String" in java..

Homework Statement


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.


Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org


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.?
 


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.
 


the_kool_guy said:

Homework Statement


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.
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");

the_kool_guy said:
2) not even a method is invoked mean in printing, we just mention name of string class.
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.

the_kool_guy said:
3) also we even can declare its type-public/private/default/protected.

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

the_kool_guy said:
its just used in a manner of a primitive data type...
can someone please enlighten me on this topic...?
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.

the_kool_guy said:
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.?
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:
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.
 


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...
 


the_kool_guy said:
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...
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.
 


okay... its the reference StringList that i am declaring public...
wat a silly qn have i asked :mad:
 


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 Langauge Specification.
 

Similar threads

Replies
12
Views
2K
Replies
2
Views
2K
Replies
2
Views
4K
Replies
12
Views
3K
Replies
2
Views
3K
Replies
5
Views
4K
Back
Top