i++ still working for immutable Integer in Java? [duplicate] - java

This question already has answers here:
Is Integer Immutable
(12 answers)
Closed 9 years ago.
I know Integer is immutable in Java. But I tried this:
Integer i = 4;
i++;
System.out.println(i); // output is 5
Why the self-increment is still working? Did Java create a new Integer object?

i++;
is equivalent to:
i = i + 1;
which i now refers to a different Integer object (5).

Related

Java casting in wrapper Number classes [duplicate]

This question already has answers here:
Java: Why can't I cast int to Long
(3 answers)
Wrapper classes - why integer literals fail for Long but work for anything smaller
(2 answers)
Closed 1 year ago.
Why it's correct:
Long l = new Long(10);
but it's not correct:
Long l2 = 10;
I understand that int is substituted here, but why is new Long(10) correct?

Getting Integers from Char Array? [duplicate]

This question already has answers here:
How can I convert a char to int in Java? [duplicate]
(4 answers)
Java: parse int value from a char
(9 answers)
Closed 3 years ago.
I was experimenting with char arrays and integers, when I found something that confused me a lot. In Java, if you type the following code:
String inputs = "123";
char[] inputs2 = inputs.toCharArray(); // Turn String into Char Array
int a = inputs2[0];
println(a);
I would have expected for "a" to return 1 because 1 is the first element in the array. However, it returned 49. Why does this occur? Is there any way to get around this issue?

new Random(seed) generates ordered sequence of numbers [duplicate]

This question already has answers here:
Why this code is giving strange result? So Random?
(6 answers)
Why random is behaving differently here?
(3 answers)
Closed 5 years ago.
There is interesting quiz which I didn't find answer yet
Random random = new Random(-6732303926L);
for (int i = 0; i < 10; i++) {
int randomNumber = random.nextInt(10);
System.out.print(randomNumber);
}
OUTPUT: 0123456789
My question is not about why random generates the same output all time. It's clear from documentation and, for example, from this answer.
My question is why it generates ordered numbers?
Could someone describe how does it work?

Java - String.join() does not equal its string [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I have list = [1,0,1], but each number in the List are of type String. I do
if ( String.join("",list) == "101"){
return true
}
As you can see, these to Strings are equal but it won't return true.
try this: using equals
if ( String.join("",list).equals("101")){
return true
}

Juxtapose number in Int [duplicate]

This question already has answers here:
How to concatenate int values in java?
(22 answers)
Closed 7 years ago.
I'm a beginner in Java. I have a simple question :
int EPSGcode = 0;
int coordinateReferenceSystem = 326;
int fuseauUTM_l = 30;
I would like to juxtapose "coordinateReferenceSystem" and "fuseauUTM_l" in ESPGcode.
I get EPSGcode = 356, but I want EPSGcode = 32630...
Simple question, any ideas ?
Concatenate two numbers as a String and parse that String back to int.
EPSGCode = Integer.parseInt(""+coordinateReferenceSystem+fuseauUTM_1);

Categories

Resources