java declare two string values in single program

java declare two string values in single program

dear friends, If you declare two string values in single program like

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

{ String s=new String("hello"); s=new String("world"); System.out.println(s); }}

I am getting output: "world".what is the reason why am not getting the string value "happy".please help me am new to java

View Answers

March 14, 2012 at 4:13 PM

String s=new String("hello"); when the above statement is executed a String object is created and s is pointing to that object.

s=new String("world"); Now a new String object is created whose value is "world" and s is now pointing to the new object whose value is "world" .

So,now no reference is pointing to "hello" object but the object is not removed. And when u say System.out.println(s), it indirectly mean as System.out.println(s.toString());









Related Tutorials/Questions & Answers:
Declare string array in Java (one and two dimensional)
Declare string array in Java (one and two dimensional... will learn how to declare string array in java. Java provides many ways to declare... the elements. ADS_TO_REPLACE_2 Above program also teaches you how to declare two
How to declare String array in Java?
Following example will show you how to declare string array in java...] + " "); } //declare and initialize two dimensional array String dim2... elements of one dimensional array and two dimensional array. 1. String arr
Advertisements
write a program which asks the user for two int values and calculates their sum.
write a program which asks the user for two int values and calculates their sum.  The title says it all, an example is (1,2) > 3
String program - Java Beginners
String program  write a program to accept a word. Pass it to the function magic(String x). The function checks the string for the presence of consecutive letters. If two letters are consecutive in any position the function
java string vowel program
java string vowel program  java string vowel program   Please visit the following links: Display the word that begins with vowel Java Count Vowels
Multiple values for a single parameter
Multiple values for a single parameter   ... multiples values for a single parameter like in checkboxes. We are going to make one... from where the values will be passed to the controller. In this program we have
string program
string program  wap to find single occurrence of given string in java without using any predefined method
sorting an array of string with duplicate values - Java Beginners
string with duplicate values  How to check the string with duplicate values
Java radio buttons and button groups two values
Java radio buttons and button groups two values  hi i have a problem...","root","K13nn0r"); ResultSet rs=null; String SQL="select...=st.executeQuery(SQL); rs.last(); int k=rs.getInt(1)+1; String k2=rs.getString
Java radio buttons and button groups two values
Java radio buttons and button groups two values  hi i have a problem...","root","K13nn0r"); ResultSet rs=null; String SQL="select...=st.executeQuery(SQL); rs.last(); int k=rs.getInt(1)+1; String k2=rs.getString
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
how to store array values into two different tables in java?
how to store array values into two different tables in java?  I have... and now I want to store these values in two different tables(i.e store 2 array values in one table and remaining two values in another table
java program for string tokenzing and parsing
java program for string tokenzing and parsing  String s="height=128 weight=22 gender=male"; i need to convert this into height="128" weight="22" gender="male" normally if we print "hello" means we use System.out.println("\"Hello
java program for string tokenzing and parsing
java program for string tokenzing and parsing  String s="height=128 weight=22 gender=male"; i need to convert this into height="128" weight="22" gender="male" normally if we print "hello" means we use System.out.println("\"Hello
A Program To Reverse Words In String in Java
A Program To Reverse Words In String in Java  A Program To Reverse Words In String in Java for example: Input:- Computer Software Output... Tell Me This Program.  Here is an example that reverse the words
sorting an array of string with duplicate values - Java Beginners
String of Array  What is mean by string of array? And how one can add, delete the records from database in string format
sorting an array of string with duplicate values - Java Beginners
sorting an array of string  Example to sort array string
Reverse String Program in Java
Reverse String Program in Java We are going to describe about reverse string program in java. In this example reverses a string entered by the user. We... A program to find the largest word in String Java
sorting an array of string with duplicate values - Java Beginners
sorting an array of string with duplicate values  I have a sort method which sorts an array of strings. But if there are duplicates in the array it would not sort properly
string program
string program  write a program to print the pattern p r o program r a m
Swapping of two numbers
program to calculate swap of two numbers. Swapping is used where  you want to interchange the values. This program will help you to increase your programming ability. In this program we will see how we can swap two numbers. We can do
How to declare array in Java?
How to declare array in Java?  Hi, How to declare array in Java? Thanks   Hi, Following is a code of declaration of String array: String[] javaTechnologies = new String[]{"Java", "JSP", "Servlet", "JPA"}; Example
string program
string program  reads sentence and find the average length of word... AverageLengthOfWords { public static void main(String[] args...); System.out.print("Enter sentence: "); String st=input.nextLine(); String str
java program to compare two hashmaps and check whether they are equal or not
java program to compare two hashmaps and check whether they are equal or not  java program to compare two hashmaps and check whether they are equal
To get the String between two strings
To get the String between two strings  How to get string between two strings repeatedly using java?   Hi, Please tell me more about what do you want to achive.ADS_TO_REPLACE_1 Thanks
Program using String functions - Java Beginners
Program using String functions  Write a program to accept a sentence... { public static void main(String[] args) { int count = 0...); System.out.print("Enter string:"); String st = input.nextLine
Merge two list into a single list
Description: This example demonstrate how to merge two lists into a single... and the mergelist funtion merge the two lists into a single list. Code: # include... ( struct node *p ) { printf("The data values in the list are\n"
Find String Values of Cell Using POI
Find String Values of Cell Using POI       In this program we are going to find the String... FindStringCellsValues.java C:\POI3.0\exmples\execl>java FindStringCellsValues example.xls String
String Function Program
String Function Program  The string function program is just an application of the string manipulation functions in JAVA. The program is designed to count the number of one-letter word, two-letter word, and three-letter words
Two Dimensional Array Program
Two Dimensional Array Program Using Nested for loop       This is a simple Java array  program . In this session we will teach how to use of a two dimensional array
Two Dimensional array program
Two Dimensional array program  consider a two dimensional array... and the first become the last.let the program output elements of the first array...]+" "); } } } public static void main(String[] args) throws Exception { int rows
Two Dimensional Array Program
;    This is very simple program of Java. In this lesson we will learn how to display arrange form of two dimensional array program... a integer for array declaration Two dimensional array program. We are going
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
How to declare a Combobox without using a string in its declaration?
How to declare a Combobox without using a string in its declaration?  What i mean to ask is how can i declare a combobox first and initialise the values later? For example JComboBox x= new JComboBox(); ... String s={"Alpha
String reverse program
String reverse program  write a java program for reverse string? if the string is "Hi welcome to hyderabad" the reverse string is "hyderabad... for help. You can split the String into String array using split method of String
Java String
Java String       In this section, you will learn about the java string. Java String is a set.... All String literals are instances of this class and two string literals
Program to swap the string
!!   Here is an example that prompts the user to enter two string and then swaps both the string without using third variable. import java.util.*; class...Program to swap the string  Hi, can any one tell me the code to swap
Out of bounce exception in the string reverse program - Java Beginners
this program then that time used c:/>java StringReverseExample rose if you...Out of bounce exception in the string reverse program  In the given program below this is taken from ur site but when i am executing it.It is giving
Date Class with Single String parameter constructor example
.style1 { font-size: medium; } Date class with single string parameters to the Date() constructor example:- The Date class have a single string parameterized constructor. If user pass a string parameter in the date class
java
java declare two string values in single program  dear friends, If you declare two string values in single program like class StringDemo... new to java   String s=new String("hello"); when the above statement
python merge two dictionaries add values
python merge two dictionaries add values  Hi, In Python I have two dictionaries with same keys. I want to merge the two dictionaries and add the values (int values) into the merged dictionary. For example student marks in three
Generate Random Integer Between Two Values in Scala
Generate Random Integer Between Two Values in Scala  Hi, I am working on the SCALA project and want to generate random integer between two numbers. How to Generate Random Integer Between Two Values in Scala? Thanks   
Generate Random Integer Between Two Values in Scala
Generate Random Integer Between Two Values in Scala  Hi, I am working on the SCALA project and want to generate random integer between two numbers. How to Generate Random Integer Between Two Values in Scala? Thanks   
String Arrays Java
String Arrays Java      ... of values of a single type. Array is a collection of similar data types. It means if you have declared an array as string then it can only store the string values
how to get string between two characters
how to get string between two characters  hgow to get string from between two characters. ex:[email protected] from the above word i want a string between characters . and @ send the required method and give better example

Ads