知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

      • Theory:Scanning the input
      • Theory:Arithmetic operations
      • Theory:Integer types and operations
      • Theory:Increment and decrement
      • Theory:Relational operators
      • Theory:Ternary operator
      • Theory:The while and do-while loops
      • Theory:Branching statements
      • Theory:Characters
      • Theory:String
      • Theory:Boolean and logical operators
      • Theory:Sizes and ranges
        • Numbers
        • Characters
        • Booleans
        • Conclusion
      • Theory:Switch statement
      • Theory:Declaring a method
      • Theory:The main method
      • Theory:Type casting
      • Theory:Primitive and reference types
      • Theory:Array
      • Theory:Arrays as parameters
      • Theory:Iterating over arrays
      • Theory:Multidimensional array
      • Theory:Final variables
    • Java OOP

    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Java basic
Jim
2022-08-09
目录

Theory:Sizes and ranges

This topic teaches the classification and properties of primitive types provided by Java. Maybe you already know some of them. There are several groups of basic types divided by meaning. Types from the same group operate similarly, but they have different sizes and, as a result, represent different ranges of values.

You do not need to know all this information by heart because it is easy to find it in the documentation or Google it. But a common understanding of these concepts is essential in job interviews and practice.

# Numbers

Java provides several types for integers and fractional numbers. These types are often used in arithmetic expressions.

Integer numbers (0, 1, 2, ...) are represented by the following four types: long, ⁣int, short, and byte(from the largest to the smallest). These types have different sizes and may represent different ranges of values. The range of an integer type is calculated as −(2^{n−1})−(2n−1) to (2^{n−1})−1(2n−1)−1, where n*n* is the number of bits. The range includes 0, so we subtract 1 from the upper bound.

  • byte: size 8 bits (1 byte), ranging from -128−128 to 127127;
  • short: size 16 bits (2 bytes), ranging from -32768−32768 to 3276732767;
  • int: size 32 bits (4 bytes), ranging from −(2^{31})−(231) to (2^{31})−1(231)−1;
  • long: size 64 bits (8 bytes), ranging from −(2^{63})−(263) to (2^{63})−1(263)−1.

The sizes of types are always the same. They do not depend on the operating system or hardware and cannot be changed.

The most commonly used integer types are int and long. Try to use int if it suits your purposes. Otherwise, use long.

int one = 1;
long million = 1_000_000L;
1
2

Floating-point types represent numbers with fractional parts. Java has two such types: double (64 bits) and float (32 bits). These types can store only a limited number of significant decimal digits (~6-7 for float and ~14-16 for double). Usually, you will use the double type in practice.

double pi = 3.1415;
float e = 2.71828f;
1
2

When you declare and initialize a float variable, mark the assigned value with the special letter f. Similarly, a long value is marked with the letter L.

# Characters

Java has a type named char to represent letters (uppercase and lowercase), digits, and other symbols. Each character is just a single letter enclosed in single quotes. This type has the same size as the short type (2 bytes = 16 bits).

char lowerCaseLetter = 'a';
char upperCaseLetter = 'Q';
char dollar = '$';
1
2
3

Characters represent symbols from many alphabets, including hieroglyphs and other special symbols.

# Booleans

Java provides a type called boolean, which can store only two values: true and false. It represents only one bit of information, but its size is not precisely defined.

boolean enabled = true;
boolean bugFound = false;
1
2

You will often use this type in conditionals and as a result of comparing two numbers.

# Conclusion

  • There are several types for integers and fractional numbers;
  • Integer numbers are represented by long, ⁣int, ⁣short, and byte;
  • Floating-point types represent numbers with fractional parts: double (64 bits) and float (32 bits);
  • char type represents letters (uppercase and lowercase), digits, and other symbols;
  • boolean stores only true and falsevalues.

Remember that knowledge of sizes and ranges of data types may help you with interviews.

编辑 (opens new window)
#Java basic
上次更新: 2022/09/26, 16:55:15
Theory:Boolean and logical operators
Theory:Switch statement

← Theory:Boolean and logical operators Theory:Switch statement→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式