answersLogoWhite

0


Best Answer

You can have two String variables (note that String variables are object references) refer to the same String object like so:

String str1 = "Hello";

String str2 = str1;

Now the str1 and str2 are references for the same String object containing the word "Hello".

If you actually want a new String object with a copy of the contents of the original String, you use the String constructor that takes a String argument, like so:

String str3 = new String(str1);

Now str1 and str3 refer to SEPARATE String objects that happen to contain the same sequence of characters (the word "Hello").

Since Strings objects in Java are immutable, they can be shared without worrying about the contents used by one variable being upset by the use through another variable as might happen with char[] arrays in C or C++ so the first method is probably sufficient for most cases.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

You can do that by returning the string from one method and assigning it to the other.

Ex:

public String getName() {

return "Rocky";

}

public String getFullName() {

return getName() + "Balboa";

}

Here the output from the method getName() is assigned to the method getFullName()

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you copy a string from one method into another string in another method in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What method that returns string representation of the contents of the variable?

Since the question is in the Java category: in Java, the method is called toString(). This method will automatically be invoked if you implicitly convert an object to String type, for example: "The answer is: " + myObject In this example, the String concatenation (the plus sign) forces the object, myObject, to type String - to do this, the object's toString() method will be called.


How do you use indexOf in java?

indexOf is a method of the String class. Since the indexOf method is overloaded, I will be using the indexOf(String str) version in this example. According to the API Documentation, this method "Returns the index within this string of the first occurrence of the specified substring." So, if you wanted to find the position of the letter 'v' in the String 'Java' and print it out, you would do this: String str = "Java"; int i = str.indexOf("v"); System.out.println(i); If the character you passed in the indexOf method does not exist in the String, indexOf would return a -1 (negative one).


How nesting of methods is done in java?

No, Java only allows a method to be defined within a class, not within another method.


Java program to convert lowercase to uppercase?

You can use the toUpperCase() method on a String to convert any String to all uppercase.


How do you use the Scanner class to store two words or more in a String variable then the next input in another String variable in java?

Scanner scan = new Scanner("[input method]"); // input method could they key board //(System.in) or a String String one = scan.next() + scan.next(); // get the first 2 words and concatenate String two = scan.next(); // the remaining word

Related questions

What are some ways in order to perform String comparison in Java?

String comparison in Java features four ways. These ways are String comparison using equals method, equalsIgnoreCase method, CompareTo method, and CompareToIgnoreCase method.


Does passing static strings as parameters to a method create a copy of the String or passes its reference?

Java always follows a pass by value approach.


How do you campaire Strings in java?

String class in Java has an 'equals' method that can be used to compare strings.


What method that returns string representation of the contents of the variable?

Since the question is in the Java category: in Java, the method is called toString(). This method will automatically be invoked if you implicitly convert an object to String type, for example: "The answer is: " + myObject In this example, the String concatenation (the plus sign) forces the object, myObject, to type String - to do this, the object's toString() method will be called.


How do you use indexOf in java?

indexOf is a method of the String class. Since the indexOf method is overloaded, I will be using the indexOf(String str) version in this example. According to the API Documentation, this method "Returns the index within this string of the first occurrence of the specified substring." So, if you wanted to find the position of the letter 'v' in the String 'Java' and print it out, you would do this: String str = "Java"; int i = str.indexOf("v"); System.out.println(i); If the character you passed in the indexOf method does not exist in the String, indexOf would return a -1 (negative one).


How can find the length of a string in java without using fun?

Use length() method. Example: "java".length();


How nesting of methods is done in java?

No, Java only allows a method to be defined within a class, not within another method.


Java program to convert lowercase to uppercase?

You can use the toUpperCase() method on a String to convert any String to all uppercase.


How do you input the number of characters in java?

".length()". The . length method is inherited from the String class.


What is use of stringargs in main method?

Because Java takes only string values


How can someone convert string to int in Java?

To convert string to int in Java, the easiest way is to simply use the method Integer.parseInt(). For more information how to do this, refer to the integer class documents.


How do you use the Scanner class to store two words or more in a String variable then the next input in another String variable in java?

Scanner scan = new Scanner("[input method]"); // input method could they key board //(System.in) or a String String one = scan.next() + scan.next(); // get the first 2 words and concatenate String two = scan.next(); // the remaining word