When did Java start the default value or variable initialization? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

When did Java start the default value or variable initialization?

For example, if I declare a vaiable boolean flag; (not initialized), then at next lines below if I write System'.out.println(flag); Java will give error message: The variable flag might have not been initialized. I forgor. Have you ever told me that default value of boolean variabble if not initialized when declared is false?

13th Nov 2022, 1:44 PM
Oliver Pasaribu
Oliver Pasaribu - avatar
2 Answers
+ 4
Local variable are not initialized by default at opposite as static and class member variables
13th Nov 2022, 2:52 PM
KrOW
KrOW - avatar
+ 4
//See here no values set, getting defaults: class defaultDemo { int x; double y; String s; boolean b; } class xyz { public static void main(String[] a){ defaultDemo obj = new defaultDemo(); System.out.print(obj.x+"\n"+ obj.y+"\n"+ obj.s+"\n"+ obj.b); } } output: 0 0.0 null false //edit: static boolean b ; also will have false
13th Nov 2022, 2:55 PM
Jayakrishna 🇮🇳