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.


Need of interface in java?

Interfaces are used in Java to accomplish most of the goals of Multiple Inheritance. For several reasons, Java only supports Single Inheritance for classes - i.e. a class can have only a single parent. The use of Interfaces is how Java attempts to implement most of the positives of the concept of Multiple Inheritance while avoiding its pitfalls.


How do we declare objects?

In Java, write the name of a class, followed by a variable name. For example:String myName;This won't actually create the object; you also need to create the object:myName = new("Hi, it's me.");These two parts (declaration, and object creation) can be combined:String myName = new String("Hi, it's me.");(In the case of a String, in Java you can just assign a quoted string, instead of using the new operator. However, I wanted to show the general syntax for object creation, that works for other kinds of objects too.)In Java, write the name of a class, followed by a variable name. For example:String myName;This won't actually create the object; you also need to create the object:myName = new("Hi, it's me.");These two parts (declaration, and object creation) can be combined:String myName = new String("Hi, it's me.");(In the case of a String, in Java you can just assign a quoted string, instead of using the new operator. However, I wanted to show the general syntax for object creation, that works for other kinds of objects too.)In Java, write the name of a class, followed by a variable name. For example:String myName;This won't actually create the object; you also need to create the object:myName = new("Hi, it's me.");These two parts (declaration, and object creation) can be combined:String myName = new String("Hi, it's me.");(In the case of a String, in Java you can just assign a quoted string, instead of using the new operator. However, I wanted to show the general syntax for object creation, that works for other kinds of objects too.)In Java, write the name of a class, followed by a variable name. For example:String myName;This won't actually create the object; you also need to create the object:myName = new("Hi, it's me.");These two parts (declaration, and object creation) can be combined:String myName = new String("Hi, it's me.");(In the case of a String, in Java you can just assign a quoted string, instead of using the new operator. However, I wanted to show the general syntax for object creation, that works for other kinds of objects too.)


What Java header file meaning?

Source code written in Java is simple. There is no preprocessor, no #define and related capabilities, no typedef, and absent those features, no longer any need for header files. Instead of header files, Java language source files provide the declarations of other classes and their methods.


What powder game doesnt need java?

Any program written for the Java technology needs Java. Any program NOT written for Java DOESN'T need it.


How do you create object name from a string in java?

Creating an object from its class name known as String format can be done using Java Reflection. See the following code: String name = "com.fullpath.MyClass"; // need full path Class cl = Class.forName(name); java.lang.reflect.Constructor co = cl.getConstructor(classParm); MyRequiredObject myRequiredObject = (MyRequiredObject) (co.newInstance(null)); Please note: in case the constructor require parameters, you should pass the newInstance function an array of objects


What data type is string is suited to represent cost of a car?

String is not appropriate for that. While it is possible to store a number in a string, additional conversions need to be done if you need the number for calculations. Instead, use an integer data type (if no decimals are required), or a real data type (if decimals are required). In Java, for example, you could use int (no decimals) or double (allows decimals).


Why to use 'import' in java?

The import statement in Java allows to refer to classes which are declared in other packages to be accessed without referring to the full package name. You do not need any import statement if you are willing to always refer to java.util.List by its full name, and so on for all other classes. But if you want to refer to it as List, you need to import it, so that the compiler knows which List you are referring to. Classes from the java.lang package are automatically imported, so you do not need to explicitly do this, to refer to String, for example.


What java do you need to play the free Minecraft?

you need java 1.5 to run mindcraft