• Register
1 vote
42 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,240 points

Related questions

1 vote
2 answers 35 views
35 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 18 views
18 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, 2020 MallikaShaik672 500 points
1 vote
1 answer 20 views
20 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 23 views
23 views
I am not knowing how to create a date object in Java and how to set year, month to it. I have a numerous doubts while practically writing programs in java
asked Dec 21, 2020 ZarinaShaik 320 points
0 votes
1 answer 10 views
10 views
Problem: hi I am studying and practicing JAVA these days .My passion is to understand things in a better way. I think it’s helping me a little bit more. Anyway, today I’ve come up with another question regarding Java conversion. I understand about iterable in java . i dont understand how to object not iterable in java.
asked Nov 2, 2020 Mashhoodch 5.7k points
0 votes
1 answer 49 views
49 views
Problem: This Question all of Java builtin standard, built-in objects, including their methods and properties. The term "global objects" (or standard built-in objects) here is not to be confused with the global object. Here, "global objects" refer to objects in the global scope. any one tell me about how to retrives built in objects
asked Nov 3, 2020 Mashhoodch 5.7k points
0 votes
1 answer 28 views
28 views
Problem: The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements The Answer will help me to understand about Java OOP'S concepts with examples. Here we discuss about what are the features of Object ...
asked Nov 6, 2020 Mashhoodch 5.7k points
0 votes
1 answer 30 views
30 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, 2020 Daniel Anderson 4k points
1 vote
1 answer 27 views
27 views
Problem: I kept on getting this error when building for Android (shadow demo): Building DEX Failed! G:\Unity\JavaPluginSample\Temp/StagingArea> java -Xmx1024M -Djava.ext.dirs="G:/AndroidSDK/android-sdk_r09-windows\platform-tools/lib/" -jar "G:/AndroidSDK/android- ... initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine. How to solve this?
asked Mar 27, 2020 LizzyM 6.1k points