Types: char and boolean

Java Development (13 Part Series)

1 Getting Back to Java: A Journey Through One of the Most Versatile Languages
2 How to Set Up Your Environment to Start Java Development
9 more parts…
3 IntelliJ IDEA on Ubuntu: Begin Your Journey into Java Development!
4 Your First Java Project in IntelliJ IDEA: Creating a “Hello World” in Java
5 Exploring Classes and the Main Method in Java
6 Exploring Class Organization with Packages in Java
7 Variables and Data Types in Java
8 Understanding float and double in Java
9 Types: char and boolean
10 Exploring Non-Primitive Types in Java: A Dive into Object-Oriented Programming
11 Understanding Strings and Arrays in Java
12 Exploring the for Loop in Java
13 Exploring While and DoWhile in Java: Master Loop Structures with Practical Examples

After learning about the double and float types, it’s time to dive into two other essential primitive types in Java: char and boolean. Ready to explore?

The char Type

The char type is ideal for representing a single Unicode character. This means you can store any character—letters, numbers, or symbols—using it. There are two ways to assign a value to a char variable:

  1. Using single quotes, like 'A', 'Ω', or 'B'.
  2. Using a numeric value, corresponding to the character in the Unicode table. For example, the code below stores the letter “A” using its Unicode value:
char letterA = 65;
char letterA = 65;
char letterA = 65;

Enter fullscreen mode Exit fullscreen mode

Want to see char in action? Check out this example:

public class PrimitiveTypes {
public static void main(String[] args) {
char letterA = 'a';
char capitalA = 'A';
System.out.println(letterA); // a
System.out.println(capitalA); // A
}
}
public class PrimitiveTypes {
    public static void main(String[] args) {
        char letterA = 'a';
        char capitalA = 'A';
        System.out.println(letterA); // a
        System.out.println(capitalA); // A
    }
}
public class PrimitiveTypes { public static void main(String[] args) { char letterA = 'a'; char capitalA = 'A'; System.out.println(letterA); // a System.out.println(capitalA); // A } }

Enter fullscreen mode Exit fullscreen mode

The boolean Type

The boolean type is one of the simplest—and most useful! It represents only two possible values:

  • true
  • false

This variable type is essential for making decisions and controlling the program’s flow, especially with conditionals like if and while. In Java, unlike some other languages where 0 and 1 are used to represent false and true, we explicitly use true and false.

An interesting detail is that boolean variables often have names that resemble questions. Internationally, they typically start with “is,” while in some contexts, prefixes like “eh” might be used. Examples:

public class PrimitiveTypes {
public static void main(String[] args) {
boolean isJavaFun = true; // Examples: isRunning, isActive
boolean isAdult = false; // Examples: isTrue, isReady
}
}
public class PrimitiveTypes {
    public static void main(String[] args) {
        boolean isJavaFun = true; // Examples: isRunning, isActive
        boolean isAdult = false; // Examples: isTrue, isReady
    }
}
public class PrimitiveTypes { public static void main(String[] args) { boolean isJavaFun = true; // Examples: isRunning, isActive boolean isAdult = false; // Examples: isTrue, isReady } }

Enter fullscreen mode Exit fullscreen mode

Now that you know how these types work, you can use them to make your code more intuitive and functional!

Java Development (13 Part Series)

1 Getting Back to Java: A Journey Through One of the Most Versatile Languages
2 How to Set Up Your Environment to Start Java Development
9 more parts…
3 IntelliJ IDEA on Ubuntu: Begin Your Journey into Java Development!
4 Your First Java Project in IntelliJ IDEA: Creating a “Hello World” in Java
5 Exploring Classes and the Main Method in Java
6 Exploring Class Organization with Packages in Java
7 Variables and Data Types in Java
8 Understanding float and double in Java
9 Types: char and boolean
10 Exploring Non-Primitive Types in Java: A Dive into Object-Oriented Programming
11 Understanding Strings and Arrays in Java
12 Exploring the for Loop in Java
13 Exploring While and DoWhile in Java: Master Loop Structures with Practical Examples

原文链接:Types: char and boolean

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
The worst sort of indolence is being given a choice, yet taking no initiative to change.
我们人生中最大的懒惰,就是当我们明知自己拥有作出选择的能力,却不去主动改变而是放任它的生活态度
评论 抢沙发

请登录后发表评论

    暂无评论内容