Java: Variables: char

In this tutorial I will explain the char. To do that I will use this file.


package net.tutorial.src;

public class Tutorial 
{
public static void main(String[] args)
{
System.out.println(“Hello World!”);
}
}


Like all other variables a char has to be created somewhere inside of a class.

There are 2 ways you can code a char in Java. The first one is this.


char letter = ‘\u0051′;


And this is the second one.


char letter = ‘Q';


The first part in this line is the type of variable. In this case a char.
The second part in this line of code is the name of the variable. In this case letter.
The last part of this line of code is the character which the variable will be. In both cases this is Q.
If you want to print out a number in the console you have to use a line of code like this.


System.out.println(letter);


Because this bit of the code is in the main method and the main method is static you have to add the word static in front of char. Why that is and how you should use that is something for a later tutorial though.


package net.tutorial.src;

public class Tutorial 
{
static char letter = ‘Q';

public static void main(String[] args)
{
System.out.println(letter);
}
}


 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">