Inexperienced engineer struggle record: String type edition where I was studying aiming to pass Java Silver

Introduction

It's good that Arasa, who was a part-time worker for eight years after dropping out of college, was able to change jobs to an engineer, but I didn't know right or left, so I tried hard to get Java Silver, but I'm stuck here.

First of all, I have little knowledge of Java. On the contrary, it can be said that he does not have much knowledge of programming in general. I used to play with C language as a hobby and class when I was a student, and recently I studied Python3 with Web teaching materials! It is an amateur who can get a lot of attention.

So, when it comes to Java qualifications, it seems that demand is simply high. That's all. Doesn't it seem to be suitable for object-oriented programming? I only have the impression.

I'm going to write for myself almost what I've been studying until I passed Java Silver. If you have any mistakes, please let me know for my future.

String type wonder

For the time being, when I bought an introductory book on Java and studied it, I immediately came across something interesting. Because the String type is a reference type, for example

StrTest.java


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

Is 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

And that. I was so excited that I wrote this code as a trial.

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 execute it with that in mind, the output will be hoge. Hey? ??

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

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

Apparently, the String type instance is immutable, so you can't change the value in the first place. I 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 has changed a lot! !! Why can I change the value even though it is immutable? Why can't it be changed in the previous code?

I became unconscious before and after, so when I cried to Google teacher, the explanation came out firmly. Thank you to all the senior engineers.

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

It seems that the value is being changed, and in fact 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 just means that a new character string was just bombed by some method.

Recommended Posts

Inexperienced engineer struggle record: String type edition where I was studying aiming to pass Java Silver
Aiming to acquire Java Oracle Silver, point memo (pass)
I tried to convert a string to a LocalDate type in Java
I am studying to change jobs from inexperienced to a web engineer.
How to pass Oracle Java Silver
Java SE8 Silver ~ The Road to Pass ~
[Java] Convert Object type null to String type
Java SE 8 Silver (Java SE 8 Programmer I) Pass Note
Inexperienced learning methods to get Java Silver 11
Type conversion from java BigDecimal type to String type