Byte sizes in java

Java Data Types

As explained in the previous chapter, a variable in Java must be a specified data type:

Example

int myNum = 5; // Integer (whole number) float myFloatNum = 5.99f; // Floating point number char myLetter = 'D'; // Character boolean myBool = true; // Boolean String myText = "Hello"; // String 

Data types are divided into two groups:

  • Primitive data types — includes byte , short , int , long , float , double , boolean and char
  • Non-primitive data types — such as String , Arrays and Classes (you will learn more about these in a later chapter)

Primitive Data Types

A primitive data type specifies the size and type of variable values, and it has no additional methods.

There are eight primitive data types in Java:

Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values

Источник

Читайте также:  Object constructors in javascript

Introduction to Java Primitives

announcement - icon

As always, the writeup is super practical and based on a simple application that can work with documents with a mix of encrypted and unencrypted fields.

We rely on other people’s code in our own work. Every day.

It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.

Lightrun is a new kind of debugger.

It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.

Learn more in this quick, 5-minute Lightrun tutorial:

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

The Java Programming Language features eight primitive data types.

In this tutorial, we’ll look at what these primitives are and go over each type.

2. Primitive Data Types

The eight primitives defined in Java are int, byte, short, long, float, double, boolean and char. These aren’t considered objects and represent raw values.

They’re stored directly on the stack (check out this article for more information about memory management in Java).

We’ll take a look at storage size, default values and examples of how to use each type.

Let’s start with a quick reference:

Type Size (bits) Minimum Maximum Example
byte 8 -2 7 2 7 – 1 byte b = 100;
short 16 -2 15 2 15 – 1 short s = 30_000;
int 32 -2 31 2 31 – 1 int i = 100_000_000;
long 64 -2 63 2 63 – 1 long l = 100_000_000_000_000;
float 32 -2 -149 (2-2 -23 )·2 127 float f = 1.456f;
double 64 -2 -1074 (2-2 -52 )·2 1023 double f = 1.456789012345678;
char 16 0 2 16 – 1 char c = ‘c’;
boolean 1 boolean b = true;

2.1. int

The first primitive data type we’re going to cover is int. Also known as an integer, int type holds a wide range of non-fractional number values.

Specifically, Java stores it using 32 bits of memory. In other words, it can represent values from -2,147,483,648 (-2 31 ) to 2,147,483,647 (2 31 -1).

In Java 8, it’s possible to store an unsigned integer value up to 4,294,967,295 (2 32 -1) by using new special helper functions.

We can simply declare an int:

The default value of an int declared without an assignment is 0.

If the variable is defined in a method, we must assign a value before we can use it.

We can perform all standard arithmetic operations on ints. Just be aware that decimal values will be chopped off when performing these on integers.

2.2. byte

byte is a primitive data type similar to int, except it only takes up 8 bits of memory. This is why we call it a byte. Because the memory size is so small, byte can only hold the values from -128 (-2 7 ) to 127 (2 7 – 1).

Here’s how we can create byte:

The default value of byte is also 0.

2.3. short

The next stop on our list of primitive data types in Java is short.

If we want to save memory and byte is too small, we can use the type halfway between byte and int: short.

At 16 bits of memory, it’s half the size of int and twice the size of byte. Its range of possible values is -32,768(-2 15 ) to 32,767(2 15 – 1).

short is declared like this:

Also similar to the other types, the default value is 0. We can use all standard arithmetic on it as well.

2.4. long

Our last primitive data type related to integers is long.

long is the big brother of int. It’s stored in 64 bits of memory, so it can hold a significantly larger set of possible values.

The possible values of a long are between -9,223,372,036,854,775,808 (-2 63 ) to 9,223,372,036,854,775,807 (2 63 – 1).

We can simply declare one:

long l = 1_234_567_890; long l;

As with other integer types, the default is also 0. We can use all arithmetic on long that works on int.

2.5. float

We represent basic fractional numbers in Java using the float type. This is a single-precision decimal number. This means that if we get past six decimal points, the number becomes less precise and more of an estimate.

In most cases, we don’t care about the precision loss. But if our calculation requires absolute precision (e.g., financial operations, landing on the moon, etc.), we need to use specific types designed for this work. For more information, check out the Java class Big Decimal.

This type is stored in 32 bits of memory just like int. However, because of the floating decimal point, its range is much different. It can represent both positive and negative numbers. The smallest decimal is 1.40239846 x 10 -45 , and the largest value is 3.40282347 x 10 38 .

We declare floats the same as any other type:

And the default value is 0.0 instead of 0. Also, notice we add the f designation to the end of the literal number to define a float. Otherwise, Java will throw an error because the default type of a decimal value is double.

We can also perform all standard arithmetic operations on floats. However, it’s important to note that we perform floating point arithmetic very differently than integer arithmetic.

2.6. double

Next, we look at double. Its name comes from the fact that it’s a double-precision decimal number.

It’s stored in 64 bits of memory. This means it represents a much larger range of possible numbers than float.

Although, it does suffer from the same precision limitation as float does. The range is 4.9406564584124654 x 10 -324 to 1.7976931348623157 x 10 308 . That range can also be positive or negative.

Declaring double is the same as other numeric types:

double d = 3.13457599923384753929348D; double d;

The default value is also 0.0 as it is with float. Similar to float, we attach the letter D to designate the literal as a double.

2.7. boolean

The simplest primitive data type is boolean. It can contain only two values: true or false. It stores its value in a single bit.

However, for convenience, Java pads the value and stores it in a single byte.

Here’s how we declare boolean:

boolean b = true; boolean b;

Declaring it without a value defaults to false. boolean is the cornerstone of controlling our programs flow. We can use boolean operators on them (e.g., and, or, etc.).

2.8. char

The final primitive data type to look at is char.

Also called a character, char is a 16-bit integer representing a Unicode-encoded character. Its range is from 0 to 65,535. In Unicode, this represents ‘\u0000′ to ‘\uffff’.

For a list of all possible Unicode values, check out sites such as Unicode Table.

char c = 'a'; char c = 65; char c;

When defining our variables, we can use any character literal, and they will get automatically transformed into their Unicode encoding for us. A character’s default value is ‘/u0000′.

2.9. Overflow

The primitive data types have size limits. But what happens if we try to store a value that’s larger than the maximum value?

We run into a situation called overflow.

When an integer overflows, it rolls over to the minimum value and begins counting up from there.

Floating point numbers overflow by returning Infinity:

int i = Integer.MAX_VALUE; int j = i + 1; // j will roll over to -2_147_483_648 double d = Double.MAX_VALUE; double o = d + 1; // o will be Infinity

Underflow is the same issue except it involves storing a value smaller than the minimum value. When the numbers underflow, they return 0.0.

2.10. Autoboxing

Each primitive data type also has a full Java class implementation that can wrap it. For instance, the Integer class can wrap an int. There is sometimes a need to convert from the primitive type to its object wrapper (e.g., using them with generics).

Luckily, Java can perform this conversion for us automatically, a process called Autoboxing:

Character c = 'c'; Integer i = 1;

3. Conclusion

In this article, we’ve covered the eight primitive data types supported in Java.

These are the building blocks used by most, if not all, Java programs out there, so it’s well worth understanding how they work.

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

Источник

Оцените статью