How Does Java Treat Strings Like Primitive Data Types?

  • Comp Sci
  • Thread starter the_kool_guy
  • Start date
  • Tags
    Java Strings
In summary: Homeworke Statement is 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 can declare its type-public/private/default/protected, it is used in a manner of a primitive data type. Can someone please enlighten me on this topic?Thanks.In summary, the String class in Java is used to store strings of letters and is treated as a primitive data type. It can be declared with different types and does not require instantiation or method invocation when used for printing.
  • #1
the_kool_guy
37
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
  • #2


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.?
 
  • #3


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.
 
  • #4


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.
 
  • #5


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...
 
  • #6


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


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


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.
 

1. What are Java strings?

Java strings are a sequence of characters that are used to represent text in Java programs. They are objects that are created from the String class and can be manipulated and compared using various methods.

2. How are strings represented in Java?

In Java, strings are represented as an array of characters. Each character in the string is stored in a specific index of the array, starting from index 0. This allows for easy access and manipulation of individual characters in a string.

3. How can I create a string in Java?

To create a string in Java, you can use the String class constructor or simply assign a string literal to a variable. For example: String myString = new String("Hello World"); or String myString = "Hello World";

4. How can I concatenate strings in Java?

You can use the concat() method to concatenate two strings in Java. Alternatively, you can also use the + operator to combine strings. For example: String fullName = firstName.concat(lastName); or String fullName = firstName + " " + lastName;

5. How do I compare strings in Java?

In Java, you can use the equals() method to compare two strings for equality. This method compares the content of the strings rather than the memory address. You can also use other methods such as compareTo() or equalsIgnoreCase() for more specific comparisons.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
946
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top