• Register
1 vote
94 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

Please log in or register to answer this question.

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,260 points

Related questions

1 vote
2 answers 45 views
45 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, 2020 Gavin 15.3k points
0 votes
1 answer 6 views
6 views
Problem: Following program output In second v.i:15 In first v.i:20 why it's not 15 in both cases.The object of Value is passed and then object reference is changed in the Second method. Second method is 15 as it should be and seems like in the First method it should also be 15
asked Apr 22 ummeshani 8.8k points
1 vote
1 answer 28 views
28 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, 2020 Gavin 15.3k points
0 votes
1 answer 7 views
7 views
Problem: How I can fix this issue: What are local and global variable in python?
asked Mar 17 Wafa Abu Yousef 6.1k points
0 votes
2 answers 27 views
0 votes
1 answer 4 views
4 views
Problem: I would like to make a function that can accept any incoming variable, regardless of type (int, double, String or other objects), and then possibly determine the type of variable and act conditionally on the type.How can I do this?
asked Apr 26 sumaiya simi 43.9k points
0 votes
1 answer 6 views
6 views
Problem: What is a class, an object and an instance in Java?
asked Apr 17 sumaiya simi 43.9k points
0 votes
1 answer 4 views
4 views
Problem: How do I check if an object is an instance of a class Java?
asked Apr 15 Humaira ahmed 50.7k points
0 votes
1 answer 4 views
4 views
Problem: Hi there! My question is regarding objects in Java. Is there any mechanism that could be used to make copies of an object automatically? If yes, please explain.
asked Mar 30 Code Learner 9.8k points