Pass a simple String Object to a class - java

I am trying to send my binary string to checkSequence, but when i display that string object, all I get is blank. Any ideas?
public class binaryGame
{
public static void main( String[] args )
{
String binaryString = new String(); // creating object I want to send
binaryString.equals(createString());
checkSequence(binaryString);
}
private static String checkSequence(String binaryString)
{
//things
}
right now, the program compiles, but the "String binaryString" is empty. I think it is just creating a new binaryString String, but not passing the object. thanks in advance.

I can't see where you initialized your string.
String.equals doesn't affect string object, it only compare them. So you need to do something like this:
binaryString = createString();

Related

How to change certain characters from a method in another class?

I am trying to have a string use a method from another class to change a certain character to a different character. However when I run my program nothing happens.
In my main class I have:
String example = "example";
Pears.mToX(example);
System.out.println(example);
and in my second class called "Pears.java" I have:
public static void mToX(String word){
word.replace("m", "x");
}
Strings are immutable, meaning they can't be changed, so when you use replace it returns the result as a new String, which you're doing nothing with. You'll need to restructure your code like so:
String example = "example";
example = Pears.mToX(example);
System.out.println(example);
And the method:
public static String mToX(String word){
return word.replace("m", "x");
}

what is the correct and best way to pass argument by reference to a method?

This is my code :
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
String str = new String("this is a text");
System.out.println(str);
getThis(str);
System.out.println(str);
}
private static void getThis(String str) {
str = "text changed";
}
}
and the output is :
this a text
this a text
I want str change after the getThis method called.
I know I should pass str by reference, and I know that this can be done by declaring the str as static and out of the main method and then call it in the method like this Main.str. But is it the correct way and standard way to pass by reference?
Java is not pass by reference, it's always pass by value. And for references.
It's passing references as values to the caller. You can do it by returning the String value from getThis() method and assigned to the same variable
public class Main {
public static void main(String[] args) {
String str = new String("this is a text");
System.out.println(str);
str = getThis();
System.out.println(str);
}
private static String getThis() {
return "text changed";
}
}
As others have stated Java is always pass by value with the slight caveat that when you pass in objects (like String) you are passing the value of a Reference to an object on the heap.
In your example, assignment has no effect outside the method and since Strings are immutable you can't really do much. If you passed in a StringBuilder then you could mutate the state of the object on the heap.
More generally instead of passing in an Object x you can pass in a wrapper object that contains a set method. Java provides an AtomicReference which allows you to do this.
In java, "references to objects are passed by value". So, any
reference to a non-primitive object that you pass will be directly
used and changes will be reflected in the original object.
Also, as a side note, Strings are immutable, so, you
will get a new String if you try to change it (Strings cannot be changed), the original one will not be changed.

"Variable may not have been initialized"

I've got a method that creates a String and another method that changes Strings
void create(){
String s;
edit(s);
System.out.println(s);
}
void edit(String str){
str = "hallo";
}
My compiler says that it "may not have been initialized".
Can someone explain this?
Variable may not have been initialized
As you define the s inside a method you have to init s in it somewhere every variable in a program must have a value before its value is used.
Another thing not less important, your code won't never work as you expected cause
Strings in java are inmutable then you cannot edit your String, so you should change your method edit(Str s).
I Change your code to something like this but i think your edit method should do another thing rather than return "hallo".
void create(){
String s=null;
s =edit(); // passing a string to edit now have no sense
System.out.println(s);
}
// calling edit to this method have no sense anymore
String edit(){
return "hallo";
}
Read more about that java is passed by value in this famous question : Is Java "pass-by-reference"?
See this simple Example showing that java is passed by value. I cannot make an example with only Strings cause Strings are inmutable. So i create a wrapper class containing a String that is mutable to see differences.
public class Test{
static class A{
String s = "hello";
#Override
public String toString(){
return s;
}
}
public static void referenceChange(A a){
a = new A(); // here a is pointing to a new object just like your example
a.s = "bye-bye";
}
public static void modifyValue(A a){
a.s ="bye-bye";// here you are modifying your object cuase this object is modificable not like Strings that you can't modify any property
}
public static void main(String args[]){
A a = new A();
referenceChange(a);
System.out.println(a);//prints hello, so here you realize that a doesn't change cause pass by value!!
modifyValue(a);
System.out.println(a); // prints bye-bye
}
}
You declare local variable s in method create, so that you need to initialized it before you use it. Remember that java does not have default value for local variable.
Init String s = "" or whatever value than your code will run normally.
try to initialize the string "s" to a null value, since you have declared a variable "s" but it has not been initialized. Hence it can't pass the reference of that variable while used as parameter.
String s = null;
Hope this helps
Give your variable S a value or as Jeroen Vanneve said "Change it to String s = null;"

Modifying String/Integer object and check if it affects a class object which has the object as its parameter

public static void main(String[] args)
{
String name = "john";
StringTest obj = new StringTest(name);
name = "peter";
System.out.println(obj.name);
}
}
class StringTest{
String name;
StringTest(String name)
{
this.name = name;
}
}
Now, since the string name has been reassigned from "john" to "peter" i expect it to print peter but it prints john. Has string being immutable causes a new object to be created when it is reassigned or what is the correct explanation for this?
Also when i try this with Integer object, the behaviour is same! Anyone please explain the reason for this behaviour
Java uses pass-by-value, which means that you pass the value of name, not the reference.
Changing the value of name after you already created the StringTest instance won't affect your StringTest in any way.
I think what throws you off here is the fact that you do not have to use the "new" keyword to create the strings. The code reads more like creating a primitive type like int or long instead of object creation. Which is what is actually taking place.
An equivalent way of creating the two strings is
String name = new String(new char[] {'j','o','h','n'});
StringTest obj = new StringTest(name);
name = new String(new char[] {'p','e','t','e','r'});
Written that way I think it is more clear why you do not achieve a side effect on the string referenced in StringTest. It has nothing to do with the fact that strings are indeed immutable.

How do I send a string to a char array in Java?

String product = Integer.toString(w);
char[] original = String.toCharArray(product);
This is the code I have so far. The error says that I can't use toCharArray on String, but I looked in the documentation, and it is a listed method, so I'm kind of stuck.
product.toCharArray()
The toCharArray is not a static method, but is a method of a string that already exists, which is why it didn't compile for you.
Here is a longer example:
public class ToCharArrayString {
public static void main(String args[]) {
//method converts complete String value to char array type value
String str = " einstein relativity concept is still a concept of great discussion";
char heram[] = str.toCharArray();
// complete String str value is been converted in to char array data by
// the method
System.out.print("Converted value from String to char array is: ");
System.out.println(heram);
}
}
If the original reason was to reverse a number, my sugguestion is
StringBuffer sb = new StringBuffer(Integer.toString(w)); System.out.println(sb.reverse().toString());

Categories