-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrangeBehaviourJava.java
More file actions
36 lines (30 loc) · 1.22 KB
/
Copy pathStrangeBehaviourJava.java
File metadata and controls
36 lines (30 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// This class is for some of strange behavior in java
// Which should be known to programmers
public class StrangeBehaviourJava {
public static void main(String[] args) {
int temp = 5;
boolean test = false;
// Strange 1:
// Code below will give error
// if (temp = 5) {
// System.out.println("Error!!");
// }
// But the above code with boolean will run
if (test = true) {
System.out.println("This runs!!");
}
// Now value of test is changed to true in if statement
System.out.println(test); // Prints true
// Strange 2: Insert unicode directly into string double quotes works
// But inserting character variable in double quotes will print variable name not value
char c = '\u0000';
String string = "Hello " + c + " World"; // This works
String string1 = "Hello " + " \u0000 World"; // This also works
String string3 = "Hello " + "c World"; // This print Hello c World
System.out.println(string3);
// Strange 3:
// Adding number (decimal or floating) with strings just perform concatenation
String s = "Hello" + 5;
System.out.println(s);
}
}