2

So everyone knows that the shortest Java program that you can write is:

public class Program{
     public static void main(String []args){

     }
}

One would suspect that this shortest program would only consist out of language keywords, chosen names (like class name / function name / parameter name) and some syntax (brackets, etc).

But when you look closely suddenly the String class pops up. So does that mean that the language Java can not live without its String class? Or even without the complete standard library? (I thought C++ is able to exist without its standard library, no?)

Also I have been looking at the String class code but nowhere was an implementation of the operators + and += (maybe others are available?) to be found. How does this work then? Is this embedded as a special case in the compiler? Which would even bind the String class even tighter to the java language?

Are there other classes that are embedded so deeply in the Java language?

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Juru
  • 1,623
  • 17
  • 43
  • First your declaration of the String array is wrong. Edit it to: public static void main(String[] args). Thats how you define the entry point to your program in java. Secondly the operator + exist for String as well as += ( which is just a short form not an operator itself) – Alboz Oct 03 '14 at 19:26
  • the Java class has methods, it doesn't define operators like +. – Alboz Oct 03 '14 at 19:28
  • Works as well with [] before args though, tested it before posting but it wasn't intentionally, rather a typo :). I know the operators exist as in "you can use them" but not in the code of the String class. – Juru Oct 03 '14 at 19:28
  • Java wouldn't be able to run without String, seeing as Object has a method of `toString()` in it. – Compass Oct 03 '14 at 19:28
  • Didn't think that far @Compass but indeed, so that would mean java also has a need for the Object class as well, as String inherits it. – Juru Oct 03 '14 at 19:31
  • String is in package `java.lang`. I could be wrong here, but I think this package is always included by default. Therefore things like String, System are always there. – Steven Oct 03 '14 at 19:32
  • Yes you are correct. lang is always imported by default, but does that mean it always requires all things in there? Maybe just a subset. What would be the absolute minimum to compile? Object + String? – Juru Oct 03 '14 at 19:33
  • Yes, looking at [lang](http://docs.oracle.com/javase/7/docs/api/java/lang/package-summary.html) if we don't include things like Class or void, we can't compile. Probably the only things we might be able to survive without are the wrappers for the primitives. – Compass Oct 03 '14 at 19:34
  • @Compass void is also a class? I thought it was just a keyword. So it is mapped during compile time to a Class then? Also something the compiler uses to return nothing then. – Juru Oct 03 '14 at 19:41
  • @Alboz How does your suggested edit of `main()` differ from what the OP already has? – Code-Apprentice Oct 03 '14 at 19:42
  • To run even the simplest program, Java also depends on the `ClassLoader`, `Class`, and basically every class from the Reflection API. As far as most programmers are concerned, this is all magic under the hood, though. – Code-Apprentice Oct 03 '14 at 19:43
  • See http://stackoverflow.com/questions/4648607/stringbuilder-stringbuffer-vs-operator for a discussion of the + operator. – Code-Apprentice Oct 03 '14 at 19:50
  • 1
    See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.1 for the Java 7 specification details for the + operator. – Code-Apprentice Oct 03 '14 at 19:54
  • @Juru there are some methods that actually return `Void.` I can't remember off the top of my head where. Googling Void Class will get you info. – Compass Oct 03 '14 at 20:05

2 Answers2

4

A Java String contains an immutable sequence of Unicode characters. Unlike C/C++, where string is simply an array of char, A Java String is an object of the class java.lang.

String is special class in Java and all String literal e.g. "abc"(anything inside double quotes) are maintained in a separate String pool, special memory location inside Java memory, more precisely inside PermGen Space.

Any time you create a new String object using String literal, JVM first checks String pool and if an object with similar content available, than it returns that and doesn't create a new object. JVM doesn't perform String pool check if you create object using new operator.

Unlike an ordinary class:

   String is associated with string literal in the form of double-quoted
   texts such as "Hello, world!". 

   You can assign a string literal directly into a String variable,
   instead of calling the constructor to create a String instance.

   The '+' operator is overloaded to concatenate two String operands.
   '+' does not work on any other objects such as your Person.java etc.

   String is immutable. That is, its content cannot be modified once it
   is created. For example, the method toUpperCase() constructs and
   returns a new String instead of modifying its existing content.

Strings receive special treatment in Java, because they are used frequently in a program. Hence, efficiency (in terms of computation and storage) is crucial.

The designers of Java decided to retain primitive types in an object-oriented language, instead of making everything an object, so as to improve the performance of the language.

Primitives are stored in the call stack, which require less storage spaces and are cheaper to manipulate. On the other hand, objects are stored in the program heap, which require complex memory management and more storage spaces.

The '+' operator, which performs addition on primitives (such as int and double), is overloaded to operate on String objects. '+' performs concatenation for two String operands. Java does not support operator overloading for software engineering consideration unlike C++ where you you can turn a '+' operator to perform a subtraction.

The '+' operator is the only operator that is internally overloaded to support string concatenation in Java. Take note that '+' does not work on any two arbitrary objects.

The JDK compiler, in fact, uses both String and StringBuffer to handle string concatenation via the '+' operator. For examples,

String msg = "a" + "b" + "c";

will be compiled into the following codes for better efficiency:

String msg = new StringBuffer().append("a").append("b").append("c").toString();

For performance reason, Java's String is designed to be in between a primitive and a class.

Alboz
  • 1,833
  • 20
  • 29
  • Nice resume. How is the + operator overloaded exactly? Is that a special treatment during compiling? Can't find much information in the source code about it: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java – Juru Oct 03 '14 at 19:45
  • 2
    @Juru I believe the + operator is implemented by the compiler. Since it isn't a method, you are unlikely to find it in the String class itself. In fact, I just remembered that I read recently that the compiler generates code which uses `StringBuilder` when it sees +. – Code-Apprentice Oct 03 '14 at 19:48
  • @Code-Apprentice I Edited the post to include the bit about how + is treated by the compiler. – Alboz Oct 03 '14 at 20:00
  • 1
    @Juru I added that part to the answer with an example. – Alboz Oct 03 '14 at 20:02
  • 1
    And for this reason it's sometimes (not always) preferable to use StringBuilder/Buffer instead of "+". For instance consider the following: "String a = ""; for (int i = 0; i < 5; i++) { a += "a"; } It would be compiled as: String a = ""; for (int i = 0; i < 5; i++) { a = new StringBuilder().append(a).append("a").toString(); } Which is less efficient than creating a single StringBuilder by yourself – Joel Oct 03 '14 at 20:04
  • Also found this interesting website: http://www.ntu.edu.sg/home/ehchua/programming/java/j3d_string.html – Juru Oct 03 '14 at 20:05
2

In the Java Language Specification, section 1.4:

1.4. Relationship to Predefined Classes and Interfaces

As noted above, this specification often refers to classes of the Java SE platform API. In particular, some classes have a special relationship with the Java programming language. Examples include classes such as Object, Class, ClassLoader, String, Thread, and the classes and interfaces in package java.lang.reflect, among others. This specification constrains the behavior of such classes and interfaces, but does not provide a complete specification for them. The reader is referred to the Java SE platform API documentation.

So all these classes are required to be present or it would not be considered standard Java.

The Object and String classes are fleshed out more fully by sections 4.3.2 and 4.3.3, respectively.

4.3.2. The Class Object

The class Object is a superclass (§8.1.4) of all other classes.

All class and array types inherit (§8.4.8) the methods of class Object, which are summarized as follows:

  • The method clone is used to make a duplicate of an object.

  • The method equals defines a notion of object equality, which is based on value, not reference, comparison.

  • The method finalize is run just before an object is destroyed (§12.6).

  • The method getClass returns the Class object that represents the class of the object.

    A Class object exists for each reference type. It can be used, for example, to discover the fully qualified name of a class, its members, its immediate superclass, and any interfaces that it implements.

    The type of a method invocation expression of getClass is Class<? extends |T|> where T is the class or interface searched (§15.12.1) for getClass.

  • A class method that is declared synchronized (§8.4.3.6) synchronizes on the monitor associated with the Class object of the class.

  • The method hashCode is very useful, together with the method equals, in hashtables such as java.util.Hashmap.

  • The methods wait, notify, and notifyAll are used in concurrent programming using threads (§17.2).

  • The method toString returns a String representation of the object.

4.3.3. The Class String

Instances of class String represent sequences of Unicode code points.

A String object has a constant (unchanging) value.

String literals (§3.10.5) are references to instances of class String.

The string concatenation operator + (§15.18.1) implicitly creates a new String object when the result is not a constant expression (§15.28).


Also consider Throwable, without which no try/catch statement could even be compiled (14.20):

Each class type used in the denotation of the type of an exception parameter must be the class Throwable or a subclass of Throwable, or a compile-time error occurs.

Community
  • 1
  • 1
Michael Myers
  • 188,989
  • 46
  • 291
  • 292