answersLogoWhite

0

Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings. Creating Strings The most direct way to create a string is to write: String greeting = "Hello world!"; In this case, "Hello world!" is a string literal-a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates a String object with its value-in this case, Hello world!. As with any other object, you can create String objects by using the new keyword and a constructor. The String class has 11 constructors that allow you to provide the initial value of the string using different sources, such as an array of characters: char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'}; String helloString = new String(helloArray); System.out.println(helloString); The last line of this code snippet displays hello. Note: The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation.

User Avatar

Wiki User

16y ago

What else can I help you with?

Related Questions

How do you delete the given string in java programming?

You usually do not need to delete a String, since when the program no longer refers to it, the garbage collector will clean it up.


Why constructor rather than classes in java?

Constructor is not an alternative to class. In Java, you create classes; the classes contain methods - including the constructor, which can be viewed as a special method. If you want to have a constructor, you need a class that surrounds it, so it's not one or the other.


Is String in java a class or object?

String is a pre-defined class in Java. For example: String s = new String("This is a string"); the variable s is now a String object since it was declared and initialized in the String class.


Name the following-A package that is invoked by default?

The java.lang package is invoked by default by all Java classes. We need not import the classes inside the Java.lang package inside our class. They are automatically imported into all Java classes. Example: java.lang.String If you have to declare an array list the import statement import java.util.ArrayList; must be there in your class declaration. If this import is not present your code using the ArrayList would not compile or work But if you are going to use a String, you can directly use it because it would be automatically imported.


Why do you use superclass init method inside your init method in a servlet?

Because, that is how all Java classes work. When a class is initialized/created all the classes it extends from (its super classes) need to be initialized as well.


What do you need JDK?

JDK stands for Java Development Kit. This contains the Java compiler, Interpreter and other tools that Java provides. It also contains all Java default packages and classes that can be used for development of Java Applications. JDK versions are continuosly released and upgraded every year by Sun Microsystems.


Explain strings are immutable in java with example?

Among other things, the immutability of a String give us a few guarantees: * A String will always be Thread-safe * Duplicate Strings can be multiple pointers to the same data (reduce memory footprint) * substring method is very fast (only need a pointer and an offset, no copying) See http://mindprod.com/jgloss/immutable.html for a full description.


How do you use Java Native Interface to access Delphi code from Java classes?

The Delphi code would need to be compiled into a DLL, and the DLL is then called from java using the JNI. See http://home.pacifier.com/~mmead/jni/delphi/JavaToDPR/ to get started.


Explain Difference between java 1.6 and java 1.7?

The Java Platform Standard Edition (SE) 7 has some bug fixes, it includes Mac OS X JDK Port, and has some other enhancements. You need the Java RE (Runtime Environment) on your system to run Java applications and appets. When you want to program in Java, you'll need the JDK (Java Development Kit). De JDK includes the Runtime Enviroment.


Character count java?

Assuming you want to count the number of characters in a String, you just need to call the length() method on the object. For example: String foo = "This is a string"; System.out.println(foo.length()); The above code would result in the number 16 being printed out.


How many classes are there in java?

there is one method only. go to command prompt and type this to know the methods. D:\java>javap java.awt.event.ActionListener Compiled from "ActionListener.java" public interface java.awt.event.ActionListener extends java.util.EventListener{ public abstract void actionPerformed(java.awt.event.ActionEvent); }


In Java, how can I call a method who's name is stored in a variable Ex: myMethod="Login3"; myMethod(userName)?

You need to look into scriptEngine and Evaluator classes.