Example tutorial of operators and type conversion in java
Type conversion:
Automatic type conversion (implicit conversion) of sorting from small to large
When a small data type is operated on a large data type, the result will be automatically converted to the large type
byte char short -->int -->long -->float -->double
Note: byte char short are not converted to each other. They participate in operations and are first converted to int type
Format: data type variable with a large range = data type value with a small range
Forced type conversion (display conversion)
Large type data can be converted if the loss of precision can be tolerated Force conversion to small type data
Format: Small range data type Variable = (Small range data type) Large range data type
Concept of operator:
For constants The symbols that operate on variables are called operators
The concept of expressions:
Using operators to connect constants and a formula that conforms to Java syntax can be called an expression
Commonly used operators: arithmetic operators, assignment operators, relational operators, logical operators, ternary operators
Arithmetic operators: + - * / % ++ --
in java The result of the division operation between two int type data is also an int. The decimal point is cut off directly
Code demonstration:
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a + b); // 30
System.out.println(a - b); // -10
System.out.println(a * b); // 200
System.out.println(a / b); // 0
System.out.println(1/2.0); //0.5
System.out.println(1/3);//0
System.out.println(2/3);//0
System.out.println("======= ====================");
// When + is used between a string and a number, it indicates a link and ultimately a new string is obtained
int d = 10;
System.out.println("Heyhey"+10);//Heyhey 10
System.out.println("Heyhey"+10+10);//Heyhey 1010
System.out.println("Heyhey"+(10+10));//Heyhey 20
System.out.println(10+10+"Heyhey");//20Heyhey
System .out.println("===========================");
System.out.println(10%3); // Find the remainder (modulo)
System.out.println(7%2);// 1
System.out.println("============== =========");
// 'a' ---> 97 'b'---> 98
// 'A' ---> 65
// '0'---> 48
System.out.println('a' + 10);// 107
System.out.println('A' + 10);// 75
System.out.println('0' + 10);// 58
System.out.println((char)('a' + 1));//b
}
++ operator:
When the ++ operator is used alone, whether the ++ symbol is on the left or right side of the variable, it means that the variable will be incremented by 1
When ++ When operators are used together, if the ++ symbol is on the left side of the variable, the emphasis is on changing it first (incrementing it by 1) and then matching it,
If it is on the right side of the variable, it emphasizes matching it first and then changing it (incrementing it by 1)
-- Operator:
When -- is used alone, whether -- is on the left or right side of a variable, it means decrementing the variable by 1
When -- is used in combination, if -- is on the left side of the variable, the emphasis is to change (decrement by 1) first and then match it,
If in On the right side of the variable, emphasize matching first and then changing (decrease by 1)
Code demonstration:
public static void main(String[] args) {
int b = 10;
System.out.println(b++);//10
System.out.println(b);/// 11
System.out.println(" ==================");
int c = 20;
System.out.println(--c);// 19
System .out.println(c);// 19
int d = 30;
System.out.println(d--);//30
System.out.println(d) ;// 29
}
Assignment operator:
Basic assignment operator:=
Extended assignment operator:+= -= *= /= %=
Change the left and the result on the right is assigned to the left
Note: the left cannot be a constant
implies a forced type conversion
Benefits: more efficient
Code demonstration:
public static void main(String[] args) {
int a = 10;
a+=2; // a = a + (2) --- > a = 10 + (2) ---> a = 12
System.out.println(a);// 12
int b = 20;
b-=2; // b = b - (2) ---> b = 18
System.out.println(b);//18
short c = 10;
//c = (short)(c + 10); // short = short + int ---> short = int
c+=10;
System.out.println(c);//20
}
Relational operators:
==(equal) !=(not equal) >(greater than) =(greater than or equal to) Relational operators are all of boolean type, either true or false
Code demonstration:
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 10;
int b = 20;
int c = 10;
System.out.println( a == b);// false
System.out.println( a == c);// true
System.out.println("======================");
System.out. println( a != b);// true
System.out.println( a != c);// false
System.out.println("============ ===========");
System.out.println( a >= b);// false
System.out.println( a >= c);/ / true
System.out.println("======================");
System.out.println( a System.out.println( a System.out.println("================ =======");
System.out.println( a > b); // false
System.out.println( a > c); // false
System .out.println("======================");
System.out.println( a System.out.println( a System.out.println("======================" );
}
Logical operators:
Logical operators are used to connect Boolean expressions and the final result value is Boolean.
In Java, it cannot be written as 3
Not: ! Operation rule: true Change false False to true
XOR: ^ Operation rules: If both sides are the same, it is false, if they are different, it is true
Code demonstration:
public static void main(String[] args) {
System.out.println(false & true );// F
System.out.println(true & false );// F
System .out.println(false & false );// F
System.out.println("=========================") ;
System.out.println(true | true ); // T
System.out.println(false | true );// T
System.out.println(true | false );/ / T
System.out.println(false | false );// F
System.out.println("====================== ====");
System.out.println(!true);// F
System.out.println(!false);// T
System.out.println("= ========================");
System.out.println(true ^ true); // F
System.out. println(false ^ true );// T
System.out.println(true ^ false );// T
System.out.println(false ^ false );// F
}
Short circuit and: &&
Short circuit or: ||
Advantage: higher efficiency
Short circuit and: &&
The basic operation rules are similar to &, the difference is that if the left is false, the right side is not executed, the result is false
short circuit or: ||
The basic operation rules are similar to |, the difference is that if the left side is true, the right side is not executed, the result true is directly returned
三Meta-expression:
Expression 1 ? Result 1 : Result 2
Execution process:
First calculate the result of expression 1
If the result is true, return result 1, otherwise return Result 2
Code demonstration: (Get the larger of two numbers. )
(int x=3,y=4,z;z = (x>y)?x:y;//The z variable stores the large number of the two numbers.)
public class Demo09Operator {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = (a > b) ? a : b;
System.out.println("c:" + c);
}
}
(Compare whether two data are the same)
public class Demo10Operator {
public static void main(String[] args) {
// Define two variables of type int
int a = 10;
int b = 20;
boolean flag = (a == b) ? true : false;
// boolean flag = (a == b);
System. out.println(flag);
}
}
(Get the maximum value among three integers)
public class Demo11Operator {
public static void main(String[] args ) {
// Define three int type variables
int a = 10;
int b = 30;
int c = 20;
// Compare the two first Large value of integer
int temp = ((a > b) ? a : b);
int max = ((temp > c) ? temp : c);
System.out.println ("max:" + max);
}
}
The above is the detailed content of Example tutorial of operators and type conversion in java. For more information, please follow other related articles on the PHP Chinese website!

JVM works by converting Java code into machine code and managing resources. 1) Class loading: Load the .class file into memory. 2) Runtime data area: manage memory area. 3) Execution engine: interpret or compile execution bytecode. 4) Local method interface: interact with the operating system through JNI.

JVM enables Java to run across platforms. 1) JVM loads, validates and executes bytecode. 2) JVM's work includes class loading, bytecode verification, interpretation execution and memory management. 3) JVM supports advanced features such as dynamic class loading and reflection.

Java applications can run on different operating systems through the following steps: 1) Use File or Paths class to process file paths; 2) Set and obtain environment variables through System.getenv(); 3) Use Maven or Gradle to manage dependencies and test. Java's cross-platform capabilities rely on the JVM's abstraction layer, but still require manual handling of certain operating system-specific features.

Java requires specific configuration and tuning on different platforms. 1) Adjust JVM parameters, such as -Xms and -Xmx to set the heap size. 2) Choose the appropriate garbage collection strategy, such as ParallelGC or G1GC. 3) Configure the Native library to adapt to different platforms. These measures can enable Java applications to perform best in various environments.

OSGi,ApacheCommonsLang,JNA,andJVMoptionsareeffectiveforhandlingplatform-specificchallengesinJava.1)OSGimanagesdependenciesandisolatescomponents.2)ApacheCommonsLangprovidesutilityfunctions.3)JNAallowscallingnativecode.4)JVMoptionstweakapplicationbehav

JVMmanagesgarbagecollectionacrossplatformseffectivelybyusingagenerationalapproachandadaptingtoOSandhardwaredifferences.ItemploysvariouscollectorslikeSerial,Parallel,CMS,andG1,eachsuitedfordifferentscenarios.Performancecanbetunedwithflagslike-XX:NewRa

Java code can run on different operating systems without modification, because Java's "write once, run everywhere" philosophy is implemented by Java virtual machine (JVM). As the intermediary between the compiled Java bytecode and the operating system, the JVM translates the bytecode into specific machine instructions to ensure that the program can run independently on any platform with JVM installed.

The compilation and execution of Java programs achieve platform independence through bytecode and JVM. 1) Write Java source code and compile it into bytecode. 2) Use JVM to execute bytecode on any platform to ensure the code runs across platforms.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
