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!

Javadevelopmentisnotentirelyplatform-independentduetoseveralfactors.1)JVMvariationsaffectperformanceandbehavioracrossdifferentOS.2)NativelibrariesviaJNIintroduceplatform-specificissues.3)Filepathsandsystempropertiesdifferbetweenplatforms.4)GUIapplica

Java code will have performance differences when running on different platforms. 1) The implementation and optimization strategies of JVM are different, such as OracleJDK and OpenJDK. 2) The characteristics of the operating system, such as memory management and thread scheduling, will also affect performance. 3) Performance can be improved by selecting the appropriate JVM, adjusting JVM parameters and code optimization.

Java'splatformindependencehaslimitationsincludingperformanceoverhead,versioncompatibilityissues,challengeswithnativelibraryintegration,platform-specificfeatures,andJVMinstallation/maintenance.Thesefactorscomplicatethe"writeonce,runanywhere"

Platformindependenceallowsprogramstorunonanyplatformwithoutmodification,whilecross-platformdevelopmentrequiressomeplatform-specificadjustments.Platformindependence,exemplifiedbyJava,enablesuniversalexecutionbutmaycompromiseperformance.Cross-platformd

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

Javaispopularforcross-platformdesktopapplicationsduetoits"WriteOnce,RunAnywhere"philosophy.1)ItusesbytecodethatrunsonanyJVM-equippedplatform.2)LibrarieslikeSwingandJavaFXhelpcreatenative-lookingUIs.3)Itsextensivestandardlibrarysupportscompr

Reasons for writing platform-specific code in Java include access to specific operating system features, interacting with specific hardware, and optimizing performance. 1) Use JNA or JNI to access the Windows registry; 2) Interact with Linux-specific hardware drivers through JNI; 3) Use Metal to optimize gaming performance on macOS through JNI. Nevertheless, writing platform-specific code can affect the portability of the code, increase complexity, and potentially pose performance overhead and security risks.

Java will further enhance platform independence through cloud-native applications, multi-platform deployment and cross-language interoperability. 1) Cloud native applications will use GraalVM and Quarkus to increase startup speed. 2) Java will be extended to embedded devices, mobile devices and quantum computers. 3) Through GraalVM, Java will seamlessly integrate with languages such as Python and JavaScript to enhance cross-language interoperability.


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

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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 English version
Recommended: Win version, supports code prompts!
