Cookie Consent by Free Privacy Policy Generator 📌 Numeric Data Types and Operations


✅ Numeric Data Types and Operations


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Java has six numeric types for integers and floating-point numbers with operators +, -, *, /, and %.

Numeric Types

Every data type has a range of values. The compiler allocates memory space for each variable or constant according to its data type. Java provides eight primitive data types for numeric values, characters, and Boolean values.

Image description

Java uses four types for integers: byte, short, int, and long. Choose the type that is most appropriate for your variable. For example, if you know an integer stored in a variable is within a range of a byte, declare the variable as a byte.

Java uses two types for floating-point numbers: float and double. The double type is twice as big as float, so the double is known as double precision and float as single precision. Normally, you should use the double type, because it is more accurate than the float type.

Reading Numbers from the Keyboard

Methods for Scanner Objects

Image description

Here are examples for reading values of various types from the keyboard:

Scanner input = new Scanner(System.in);
System.out.print("Enter a byte value: ");
byte byteValue = input.nextByte();

System.out.print("Enter a short value: ");
short shortValue = input.nextShort();

System.out.print("Enter an int value: ");
int intValue = input.nextInt();

System.out.print("Enter a long value: ");
long longValue = input.nextLong();

System.out.print("Enter a float value: ");
float floatValue = input.nextFloat();

If you enter a value with an incorrect range or format, a runtime error would occur.

Numeric Operators

The operators for numeric data types include the standard arithmetic operators: addition (+), subtraction (), multiplication (****), division (/), and remainder (%*).

When both operands of a division are integers, the result of the division is the quotient and the fractional part is truncated. For example, 5 / 2 yields 2, not 2.5, and –5 / 2 yields -2, not –2.5. To perform a float-point division, one of the operands must be a floating-point number. For example, 5.0 / 2 yields 2.5.

The % operator, known as remainder or modulo operator, yields the remainder after division. The operand on the left is the dividend and the operand on the right is the divisor. Therefore, 7 % 3 yields 1, 3 % 7 yields 3, 12 % 4 yields 0, 26 % 8 yields 2, and 20 % 13 yields 7.

The % operator is often used for positive integers, but it can also be used with negative integers and floating-point values. The remainder is negative only if the dividend is negative. For example, -7 % 3 yields -1, -12 % 4 yields 0, -26 % -8 yields -2, and 20 % -13 yields 7.

Remainder is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1. Thus, you can use this property to determine whether a number is even or odd. If today is Saturday, it will be Saturday again in 7 days. Suppose you and your friends are going to meet in 10 days. What day is in 10 days? You can find that the day is Tuesday using the following expression:

(6 + 10) % 7 is 2

The program obtains minutes and remaining seconds from an amount of time in seconds. For example, 500 seconds contains 8 minutes and 20 seconds.

Image description

The + and - operators can be both unary and binary. A unary operator has only one operand; a binary operator has two. For example, the - operator in -5 is a unary operator to negate number 5, whereas the - operator in 4 - 5 is a binary operator for subtracting 5 from 4.

Exponent Operations

The Math.pow(a, b) method can be used to compute a^b. The pow method is defined in the Math class in the Java API. You invoke the method using the syntax Math.pow(a, b) (e.g., Math.pow(2, 3)), which returns the result of a^b (2^3). Here, a and b are parameters for the pow method and the numbers 2 and 3 are actual values used to invoke the method. For example,

System.out.println(Math.pow(2, 3)); // Displays 8.0
System.out.println(Math.pow(4, 0.5)); // Displays 2.0
System.out.println(Math.pow(2.5, 2)); // Displays 6.25
System.out.println(Math.pow(2.5, -2)); // Displays 0.16
...

✅ Numeric Data Types and Operations


📈 37.64 Punkte

✅ Exploring Numeric Data Types in Rust and Go


📈 28.77 Punkte

✅ Data Types: 7 Key Data Types


📈 27.95 Punkte

✅ Numeric Data Types | Python for Beginners [13 of 44]


📈 27.26 Punkte

✅ Everything About Python Numeric Data Types: Beginner’s Guide


📈 27.26 Punkte

✅ Numeric Data Types in SQL: A Comprehensive Guide


📈 27.26 Punkte

✅ Exploring Numeric Data in Python: Integers, Floats, and Operations


📈 26.7 Punkte

✅ Apple Safari up to 4.0.1 Numeric Page Reference numeric error


📈 26.57 Punkte

✅ GitHub Security Lab: gagliardetto: Query to detect incorrect conversion between numeric types


📈 24.23 Punkte

✅ Kasr sonlar haqida (fractional numbers)(Floating-point numeric types (C# reference))


📈 24.23 Punkte

✅ Understanding Value Types and Reference Types in C#


📈 23.4 Punkte

✅ PHP 8 News: Union Types and Mixed Types


📈 23.4 Punkte

✅ Automatic Conversion between Primitive Types and Wrapper Class Types


📈 23.4 Punkte

✅ What is Value Types and Reference Types in JavaScript


📈 23.4 Punkte

✅ Containous Traefik up to 1.7.11 types/types.go API Request information disclosure


📈 21.89 Punkte

✅ Infer Types to Avoid Explicit Types


📈 21.89 Punkte

✅ Cast Various Types into Decimal Types in SQL


📈 21.89 Punkte

✅ ⚛️ Demystifying React's Types: Ref types


📈 21.89 Punkte

✅ Building RESTful APIs: Exploring CRUD Operations, Postman, JDBC, and Media Types


📈 21.33 Punkte

✅ Google upgrades Security Operations with new detection types and Gemini-powered assistants


📈 21.33 Punkte

✅ Minimum operations required to Sort the Array using following operations


📈 17.74 Punkte

✅ Tips and Tricks for Working with CrypTool 2 – Data Types (For Classic and Modern Ciphers)


📈 17 Punkte

✅ A Guide to Data Labeling and Annotating: Importance, Types, and Best Practices


📈 17 Punkte

✅ Microsoft Windows Data Access Components numeric error [CVE-2011-0026]


📈 16.32 Punkte

✅ Xamarin.Forms 101: Controls for boolean, numeric, and date and time input


📈 16.31 Punkte

✅ Xamarin.Forms 101: Controls for boolean, numeric, and date and time input | The Xamarin Show


📈 16.31 Punkte

✅ Data breaches explained: Types, examples, and impact


📈 15.49 Punkte











matomo

Datei nicht gefunden!