Java/J2EE and SOA (325 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (324 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What is the Default Value of Char in Java?

Published on Jul 16,2019 1.5K Views
18 / 72 Blog from Java Core Concepts

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Java is one of the most widely used programming languages. Learning Java can help you to understand the fundamentals of programming as well as Object Oriented Programming concepts. In Java, the default value of char is “u0000”. Let’s understand this concept in detail.

Below topics are covered in this article:

Let’s get started.

Why Knowing Default Value of Data Types is Important?

Some programming languages require declaring the variables in the program before using them. Hence, if you decide to use one such language, you should be familiar with the default value of different data types, as you might not always initialize a variable before using them in a program. As we speak of 2019, an enormous amount of programming languages exists, ranging from primitive to state-of-the-art languages. These languages can be further classified into 2 categories as follows:

  • Statically typed language
  • Dynamically typed language

Now let’s get into the details of these languages.

Statically Typed Language

In simple words, these languages consider the data types seriously and hence are declared as strict languages. While using Statically Typed languages, one important thing to remember is, the data type of all the variables used in a program is identified at compile time. In other words, type checking takes place at the time of compilation. Hence, a programmer needs to specify the data type every time while declaring a variable in a program. This arises a need to know the default values of commonly used data types as we may not always assign custom values to a variable at the time of declaration.

Example – Java, C, C++

Static typing Example

char  FirstVariable;

Dynamically Typed Language

In Dynamically Typed languages the data type of a variable is checked during runtime. Hence, mentioning the data type of a variable at the time of declaration is not required. Because of this flexibility, the type of data stored in a variable can be changed over time. When dealing with dynamically typed languages, knowing default values is not important.

Example – Python

Dynamic typing Example –

FirstVariable = 'Hello, this is a String type variable'
print(type(FirstVariable))
a = 10
b = 20
FirstVariable = a + b
print(type(FirstVariable)

 

Output:

<class 'str'>  #Output of first print statement
<class 'int'>  #Output of second print statement

Note: From the above output, we can conclude that initially, the type of variable FirstVariable was String. Once we assign an integer value to the same variable it’s type changed from String to Integer.

Now, let’s see what is the Default value of char in Java with the help of an example.

 The Default value of ‘Char’

Since Java is a statically typed language, variables should be declared before they can be used in a program. When we declare a variable without assigning any custom initial value it comes with a default value. The default value of different data types is different. To know more about various Data Types and their default values you refer to this post.

Before knowing the default value of a particular data type, we need to know if it’s a primitive or a user-defined data type. Having this information helps us to know where we can find more information about the data type. As primitive data types are already defined by a programming language we can find more information about it in the documentation provided by the programming language we are using.

Also, as we are concerned about the default value of char in Java, and since Char is a primitive data type we can refer the Java Documentation. For more information about user-defined data types, you can refer to the documentation provided by the developer of that class.

Char short for a character is a single Unicode character of size 16-bit, which can hold a single value enclosed in ‘ ’.

Syntax:

DataType Variablename = 'value';

Example:

char HelloWorld = 'a';

Verifying the Conclusion

The minimum value char can hold is ‘u0000‘ which is a Unicode value denoting ‘null‘ or 0 in decimal. The maximum value it can hold is ‘uffff‘ or 65,535 inclusive. The minimum value which is ‘u0000’ is also the default value of char. You might be wondering what ‘u0000’ really mean? Why the default value is not ‘a’ or ‘b’ or any other character why only ‘u000’ don’t worry we’ll cover all your doubts in the next half of this post. First, let’s try printing a char type variable and let’s divide this scenario in 2 cases:

In the first case, first, declare a char type variable and print its value.

public class JavaDefaultValues {
char DeclaredVariable;  //Declaring variable 'DeclaredVariable'
public static void main(String[] args) {
JavaDefaultValues DefaultValues = new JavaDefaultValues();  //Creating object of class JavaDefaultValues
System.out.println("Value of DeclaredVariable = " + DefaultValues.DeclaredVariable);   //Printing value of DeclaredVariable
}
}

Output:

Value of DeclaredVariable =

In the output, we can see a blank space after ‘ = ’ which denotes null character. 

In the second case, we’ll declare a Char type variable and initialize it with the default value and print its value.

public static void main(String[] args) {
JavaDefaultValues DefaultValues = new JavaDefaultValues();   //Creating object of class JavaDefaultValues
char InitialisedVariable = 'u0000';    //Initialising variable 'InitialisedVariable'
System.out.println("Value of DeclaredVariable = " + DefaultValues.DeclaredVariable);   //Printing value of DeclaredVariable
System.out.println("Value of InitialisedVariable = " + InitialisedVariable);     //Printing value ppf InitialisedVariable
 }

Output:

Value of DeclaredVariable =
Value of InitialisedVariable =

From the above output, we can see that we received a similar output.

System.out.println(DefaultValues.DeclaredVariable == InitialisedVariable);

After adding the following line of code which compares the value of our 2 variables, we receive ‘true’ on the output screen which verifies our conclusion.

We can see ‘true’ on output screen which is a result of the statement in which we compared the value of 2 variables. You can try this by yourself. The sample code is given below.

public static void main(String[] args) {
JavaDefaultValues DefaultValues = new JavaDefaultValues();     //Creating object of class JavaDefaultValues
char InitialisedVariable ='u0000';      //Initialising variable 'InitialisedVariable'
System.out.println("Value of DeclaredVariable = " + DefaultValues.DeclaredVariable);   //Printing value of DeclaredVariable
System.out.println("Value of InitialisedVariable = " + InitialisedVariable);          //Printing value ppf InitialisedVariable
System.out.println(DefaultValues.DeclaredVariable == InitialisedVariable);           //Checking if values are equal
}
}

With this, let’s dive deeper into this article to understand the concept of unicode.

Understanding Unicode

Unicode is an international encoding standard which is used with different languages. With the help of Unicode, each digit, letter or symbol is designated a unique numeric value that applies across different platforms and programs. First, let’s talk about what is character encoding? Why we need common encoding systems? Is Unicode the only encoding standard available? What’s the difference between ASCII and Unicode?

When Characters, letters, words symbols are used in a program, they can’t be stored in the digital device as it is. First, it is converted into a numeric or Hex value using character encoding. If my laptop uses an encoding system and my other desktop uses a different encoding system, the text visible on my laptop might appear differently on my desktop.

Hence, having a common encoding system is important. Initially, American Standard Code for Information Interchange ASCII was used as a standard encoding scheme but it was only able to cover 128 characters (0 – 127) which include English language, punctuation, and some other commonly used symbols. You can have a look at the ASCII table here. This scheme was not sufficient to encode characters of all languages. At this point of time Unicode comes to play. Unicode can cover 128,000 characters. It designates Hex values to different characters. For example, we saw the default value of char is ‘u0000’ this is a Hex value, when we convert this value to decimal we get ‘0’. Similarly, the maximum value of char is ‘uffff’ if we convert this hex value to decimal value we get 65,535 which we saw earlier. As the maximum value that char can hold is ‘uffff’, it cannot represent all Unicode characters. The Unicode scheme covers all the 128 characters of the ASCII table with the same designation.

With this, we come to an end of this article on the Default Value of Char in Java. I hope this article was informative to you.

Check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. 

Got a question for us? Please mention it in the comments section of this “Default Value of Char in Java ”article and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.