Table of Contents
Constant
Binary system: full binary to one, 0~1 1+1=10 0b10011 0b0011, starting from JDK1.7, 0b is allowed as Beginning to identify a number is a binary number
Convert from decimal to binary: keep dividing by 2 to take the remainder, and then put the remainder in reverse order
System.out.println(i);
Numeric type
Integer type
Data type conversion
byte b = 100;
Operator
Arithmetic operator
i -= 2;-> i = i - 2;-> i = 3;
Home Java javaTutorial Java Basics Explained-Basic Data Types and Operations

Java Basics Explained-Basic Data Types and Operations

Jul 17, 2017 pm 02:31 PM
java type Operation

Encoding

ASCII--0~127 65-A 97-a

Western European code table---ISO-8859-1---0-255---1 character Section

gb2312----0-65535---gbk---2 bytes

Unicode encoding system---utf-8---3 bytes

中 f

bit Byte 1Byte = 8bit 1KB=1024B MB GB TB PB---Storage unit in computer

Constant

Integer constant-- -All integers 3,99,107

Decimal constants---All decimals 3.5 100.9

Character constants---Use single quotes to identify a letter, number, or symbol 'a' ' =' ' '

String constant---Use double quotes to identify one or more characters "abc" "234" "q2" ""

Boolean constant---Use Indicates logical value---true/false

Empty constant---null

5-integer, 5.0-decimal '5'-character "5"-string '5.0'- Writing error "5.0"-String

Binary system: full binary to one, 0~1 1+1=10 0b10011 0b0011, starting from JDK1.7, 0b is allowed as Beginning to identify a number is a binary number

Octal: full eights into one, 0~7, 7+1=10 It is required to start with 0 06 015

Decimal: full tenths One, 0~9

Hexadecimal: full hexadecimal one, 0~9,, A~F, 9+1=A f+1=10 It is required to start with 0x 0x5 0xad

Conversion from decimal to binary

Convert from decimal to binary: keep dividing by 2 to take the remainder, and then put the remainder in reverse order

Convert from binary to decimal: from the low bit Starting from the bit order, multiply the bit order by the power of 2, and then sum up

Convert binary to octal: starting from the low order, every three digits are divided into one group, less than three Filling the bits with 0 produces one octal number. Arrange these numbers in order to convert octal to binary: one to three --- one octal number produces three binary digits.

Convert binary to hexadecimal: the process of converting four into one

Variable

System.out.println(i);

int i = 5;---No---The variable must be declared before use

int i;

System.out.println(i);---No--The variable is in use Must be initialized before

Data type

Basic data type

Numeric type

Integer type

byte---Byte type ---1 byte--- -2^7~2^7-1 --- -128~127

byte b = 5; byte b2 = -128;

short---short integer---2 bytes--- -2^15~2^15-1 --- -32768~32767

short s = 54; short s = -900 ;

int---integer---4 bytes--- -2^31~2^31-1

int i = 100000;

int j = 100_000_000;--It is allowed starting from JDK1.7. These will be automatically ignored during compilation_ -> int j = 100000000;

int i = 00001111;---Octal

The default type of integer in Java is int

long---long integer type---8 bytes--- -2^63~2^63-1---ending with L indicates that this number is a long type number

long l = 3L;

Floating point type

float---single precision---4 bytes---must end with f

float f = 3.2f;

double---double precision---8 bytes

The default decimal type in Java is double type

double d = 3.5;

double d = 4.6D;---Yes

double d = 3.9e4; //It is scientific notation in decimal system

double d = 0x3p2; //It is hexadecimal Scientific notation -> 12

Character type

char---2 bytes--- 0 ~65535

char c = 'a';

char c = '中';

Boolean

boolean---true/false

boolean b = false;

Reference data type

Class ---class Interface ---interface Array ---[]

Data type conversion

Implicit conversion/automatic type conversion

byte b = 100;

int i = b;

long l = 63;---Yes---when the integer value is within the range of int type , you don’t need to add the ending L

Rule 1: Small types can be converted into large types---byte->short->int->long float->double

int i = 5;

float f = i;

long l = 6;

float f = l;

Rule 2: Integer Can be converted to decimal, but precision loss may occur

char c = 'a';

int i = c;

Rule 3: Character type can be converted to integer type

short s = 'a';---Yes

char c = 100;---Yes

char c = 'a' ;

short s = c;---Not possible

defines a variable c of type char. The stored data is a character. There is no need to check the specific character encoding. When assigning a value to short type, short needs to check whether the encoding corresponding to the character is within the value range of the short type. At this time, the specific encoding corresponding to the character cannot be determined. Since the value range of the short type does not completely overlap with the char type, in order to prevent If it exceeds the range, assignment is not allowed.

short s = 97;

char c = s;--not possible

Explicit conversion/forced type Conversion

long l = 54;

int i = (int)l;

double d = 3.5;

int i = (int)d ;---When converting a decimal to an integer, the decimal part is discarded directly

double type cannot store decimals accurately

Hexadecimal--Hexadecimal

Decimal-- Decimal

Octal---Octal

Binary--Binary

Operator

Arithmetic operator

+addition-subtraction*multiplication/division% modulo++auto-increment--auto-decrement+string concatenation

int i = 5210 / 1000 * 1000;--->i = 5000;

Note:

1. After the integer operation is completed, the result must be an integer

2. Integer When dividing by 0, the compilation passes and the error is reported when running---ArimeticException---Arithmetic exception

3. The result of dividing a decimal by 0 is Infinity

4. The result of 0/0.0 is NaN---Not a Number---Not a number

5. The byte/short type will be automatically promoted to the int type during operation

%Remainder operation

-5%3=-2 -4%3=-1 -3%7=-3

5%-3=2 7%-2=1 2%-8= 2

-5%-3=-2 -9%-3=0

For the remainder of a negative number, first follow the remainder operation of a positive number and look at the number to the left of the remainder sign The sign of #++/--

For ++, increment by 1 on the original basis

int i = 5;

int j = ++i;--- > i increments by 1, and then assigns the value of i to j---increments first, and then operates

int j = i++;--->Get the value of i first, 5, and increments i becomes 6, and then assign the obtained value 5 to j---operate first, then increment

int i = 3;

int j = ++i * 2;-> ; j = 8;

int j = i++ * 2;->j = 6

int i = 6;

int j = i++ + ++i;->i = 8; j = 14;

int j = ++i + i++;->i = 8; j = 14

byte b = 5;

b++;---JVM will perform forced type conversion on the result at the bottom level, and then convert the result into byte type

char c = 'a';

System.out.println(c + 4);--can

char c2 = 'd';

System.out. println(c + c2);---Promote to int type and then perform operation

+ String concatenation operation

"a" + "b"-- -> "ab"

"a" + 3---> "a3"

"a" + true-> "atrue"

2 + 4 + “f”-> “6f”

“f” + 2 + 4-> “f24”

Assignment operator

= += -= *= /= %= &= |= ^= <<= >>= >>>= ~=

int i= 5;

i + = 3; -> i = i + 3; -> i = 8;

i -= 2;-> i = i - 2;-> i = 3;

int j;

j += 4;---No

int i = 5;

i += i -= i *= 5;--> i = -15;

i = 5 + ( 5 - (5 * 5)) ;

i += i -= i *= ++ i;--->i = -20;

i += i*= i-= (i++ + --i);---> i = -20;

i = 5 + ( 5 * (5 - (5 + 5)));

byte b = 5;

b += 3;--- Yes

byte b = 125;

b += 3;---Yes--- -128

Comparison/relational operator

= =Equal!=Not equal> < >= <= instanceof

##3 == 4;-> false

instanceof---Judges the relationship between objects and classes- -Can only be used for reference data types

String s = “abd”;

System.out.println(s instanceof String);---true

System. out.println(“def” instanceof String);---true

Logical operator

is used to operate logical values

&AND|OR! NOT^XOR && short circuit with || short circuit or

true&true=true true&false=false false&true=false false&false=false

true|true=true true|false=true false|true=true false|false= false

!true=false !false=true

true^true=false true^false=true false^true=true false^false=false

For &&, If the value of the previous expression is false, then it can be determined that the value of the entire expression is false, and the operation after && will no longer be performed.

ternary/ternary/conditional operator

Logical value? Expression 1: Expression 2

If the logical value is true, execute expression 1; otherwise, execute expression 2

int i = 5, j = 7;

i > j ? System.out.println(i): System.out.println(j);---No! There must be a result after the ternary operator operation is completed!

double d = i > j ? i * 2.5 : j;---The return value types of the two expressions are either consistent or compatible

Get data from the console

import java.util.Scanner; //Written under package, above class

Scanner s = new Scanner(System.in);

int i = s. nextInt();

double d = s.nextDouble();

String str = s.nextLine();

String str2 = s.next();

The above is the detailed content of Java Basics Explained-Basic Data Types and Operations. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

How to Run Your First Spring Boot Application in Spring Tool Suite? How to Run Your First Spring Boot Application in Spring Tool Suite? Feb 07, 2025 pm 12:11 PM

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo

See all articles