How Does Java Treat Strings Like Primitive Data Types?

  • Context: Comp Sci 
  • Thread starter Thread starter the_kool_guy
  • Start date Start date
  • Tags Tags
    Java Strings
Click For Summary

Discussion Overview

The discussion revolves around the conceptual understanding of how Java treats the String class and other predefined classes, particularly in relation to access modifiers and instantiation. Participants explore the nature of String as a class, its similarities to primitive data types, and the implications of declaring types in Java.

Discussion Character

  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant questions why the String class can be used without explicit instantiation or reference, suggesting it behaves like a primitive data type.
  • Another participant clarifies that all objects in Java, including String, are references and can be instantiated, even if not with the 'new' keyword.
  • It is noted that the toString() method is invoked implicitly when printing a String, which is a point of confusion for some participants.
  • Participants discuss the ability to declare access modifiers (public/private/protected/default) for class members, questioning why this is possible for predefined classes like ArrayList.
  • One participant expresses confusion about the access modifier for a member variable, suggesting that since ArrayList is predefined, it should already be public.
  • A later reply corrects this misunderstanding, explaining that the access modifier applies to the variable itself, not the class it references.
  • Another participant mentions the special support Java provides for string concatenation and conversion, referencing the StringBuffer class and the toString method.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the treatment of the String class and access modifiers, with some clarifying misconceptions while others remain uncertain about specific aspects. The discussion does not reach a consensus on the conceptual questions raised.

Contextual Notes

Some participants may have misunderstandings about Java's object-oriented principles, particularly regarding references and access modifiers. There are unresolved questions about the implications of treating String as a primitive type and the nature of class member declarations.

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

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K