Why String is immutable in Java. This is one of the most popular String Interview questions in Java, which starts with discussion of, What is String, How String in Java is different than String in C and C++, and then shifted towards what is immutable object in Java , what are the benefits of immutable object , why do you use it and which scenarios do you use it.
This is some time also asked as "Why String is final in Java" . Though there could be many possible answer for this question, and only designer of String class can answer this , I think below two does make sense 1) Imagine StringPool facility without making string immutable , its not possible at all because in case of string pool one string object/literal e.g. "Test" has referenced by many reference variables , so if any one of them change the value others will be automatically gets affected i.e. lets say String A = "Test" String B = "Test" Few more String related post from Javarevisited, you may like:
Java Coding Conventions. Why use inner classes in java. Q: So what are inner classes good for anyway?
A: Inner classes nest within other classes. A normal class is a direct member of a package, a top-level class. Inner classes, which became available with Java 1.1, come in four flavors: Static member classesMember classesLocal classesAnonymous classes Let's take a quick look at each in turn. Briefly, a static member class is a static member of a class. Like a static member class, a member class is also defined as a member of a class. Local classes are declared within a block of code and are visible only within that block, just as any other method variable. Finally, an anonymous class is a local class that has no name. To answer your specific question, I'll focus on the member and anonymous inner classes since those are the ones you'll likely encounter and use. The object-oriented advantage Let's look at the member class.
An inner class allows us to remove that logic and place it into its own class. The organizational advantage Disadvantages? Java class file. History As of 2006[update], the modification of the class file format is being considered under Java Specification Request (JSR) 202. File layout and structure Sections There are 10 basic sections to the Java Class File structure: Magic Number General layout Because the class file contains variable-sized items and does not also contain embedded file offsets (or pointers), it is typically parsed sequentially, from the first byte toward the end.
Some of these fundamental types are then re-interpreted as higher-level values (such as strings or floating-point numbers), depending on context. Representation in a C-like programming language The constant pool The constant pool table is where most of the literal constant values are stored. Due to historic choices made during the file format development, the number of constants in the constant pool table is not actually the same as the constant pool count which precedes the table. See also Java bytecode.