[Java] Inexperienced engineer struggle: studying with the aim of passing Java Silver

2 minute read

Introduction

This is because it was good that Arasa, who had been a freelancer for eight years after dropping out of college, was able to change jobs as an engineer, but I don’t know either right or left.

First of all, I have very little knowledge of Java. On the contrary, it’s safe to say that you don’t have much knowledge about programming in general. I used to mess around with C in my hobbies and classes when I was a student, and recently I’ve studied Python3 using web teaching materials! I am an amateur with a lot of stuff.

Then, it seems that demand is simply high when asked why it is Java qualification. That’s all. It seems that object-oriented is suitable for doing things, right? There is only an impression.

I’m going to write about what I was stuck studying until I passed Java Silver for myself. If you have any mistakes, please let me know for my future.

String type wonder

For the time being, I bought a Java introductory book and was studying it. How, because String type is a reference type

StrTest.java


public class StrTest {
  public static void main(String args[]) {
    String str1 = "hoge";
    String str2 = "ho";
    str2 += "ge";
    System.out.println(str1 == str2);
  }
}

Becomes false, so when comparing strings

StrTest.java


public class StrTest {
  public static void main(String args[]) {
    String str1 = "hoge";
    String str2 = "ho";
    str2 += "ge";
    System.out.println(str1.equals(str2));
  }
}

Use equals() like

That. I was so excited that I wrote this code as a test.

StrTest.java


public class StrTest {
  public static void main(String args[]) {
      String str = "hoge";
      StrChange(str);
      System.out.println(str);
  }
  
  public static void StrChange(String str){
      str = "fuga";
  }
}

The output should now be fuga. Because it’s a reference type. If you think so and execute it, the output will be hoge. Oh~ ?

It became a question at all, so when I cryed over to Google teacher, the explanation came out firmly. Thank you, senior engineers.

https://qiita.com/chihiro/items/d3d9a028cd5dd8e84649

Apparently, String type instances are immutable, so it seems impossible to change the value in the first place. Can’t change the value…? ? ?

StrTest.java


public class StrTest {
  public static void main(String args[]) {
String str = "hoge";
System.out.println(str); // hoge
str += "hoge";
System.out.println(str); // hogehoge
str = "fuga";
System.out.println(str); // fuga
  }
}

It’s changed! !! Why can I change the value even though it is immutable? Why can it be changed, but it hasn’t been changed in the previous code?

Since I became unconscious, I tried to cry over Google teacher and a solid explanation came out. Thank you, senior engineers.

https://freelance-jak.com/technology/java/1204/

Apparently it seems that the value is being changed, but in reality it is being recreated as a new value in a new area. So definitely the String type seems to be immutable. The code I wrote earlier is that a new character string was arbitrarily bombed by some method.