• Register
1 vote
30 views

Problem:

Are the all data type has default values in Java? I am just wondering what is the default value of an integer if I write:

int a;
boolean b;
System.out.println(a);
System.out.println(b);

All data fields in an object have default values or not? If they have default values what are they?

Thanks

12 7 7
15,250 points

1 Answer

1 vote
 
Best answer

Solution:

In java, you can declare three different types of variables. They are instance, local, and class variables.

For instance variables:

# The primitive type (byte, short, int, long, float, double) variables are initialized to 0.

# Boolean initialized to false.

# char initialize to null

For Class variables:

A class variable is a field within a class declared as static often referred to as a static variable or static field. It is also the same initialized as an instance variable.

For Local variables:

Local variables do not have a default value. They must be initialized before use. A local variable is a variable defined within a method, which includes any method parameters.

In a nutshell:

Data Type:              Default Value:
boolean                 false 
char                    \u0000 
int,short,byte / long   0 / 0L 
float / double          0.0f / 0.0d 
any reference type      null

 

13 9 6
94,240 points

Related questions

1 vote
2 answers 30 views
30 views
Problem: Hello java programmers, Myself is a new student in java. I am practicing basic java methods and objects. Finding beginner-level problems and trying to solve them. I recently got a line that says, all local variables in a method have default values. What does it mean? Can you please write a simple code that makes me understand this well? Thanks guys.
asked Mar 26 Gavin 15.3k points
0 votes
1 answer 10 views
10 views
Create a Class Box Also find the Volume of the Box Given the dimensions of the box are width, height, depth. The Class Box uses parameterized Constructor to initialize the box.
asked Oct 8 MallikaShaik672 140 points
1 vote
1 answer 17 views
17 views
Problem: Folks, I hope you are doing well. I started learning java programming very recently. I am on the chapter of Data Type right now of the book from where I am taking the help. There is a problem give in my book’s exercise that: “if you want to ... . Is it really possible to do so? If so, then how? Could you please give a hint regarding the problem given above? Best regards.
asked Mar 17 Gavin 15.3k points
0 votes
1 answer 9 views
9 views
Why i am seeing this error can someone give me some explanation "Could not reserve enough space for object heap"” and how to resolve this issue.
asked Sep 14 Daniel Anderson 4k points