avatarWagner Franchin

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2678

Abstract

long</b> data type is a 64-bit two's complement integer and is used to represent whole numbers between <b>-9,223,372,036,854,775,808</b> and <b>9,223,372,036,854,775,807</b>. This data type is used for integer values that require a larger range than what int can provide. The <b>long</b> data type should be used when <b>int</b> is not enough to represent the required values.</p><p id="be90">Example:</p><div id="93ea"><pre><span class="hljs-type">long</span> population = <span class="hljs-number">7</span>_594_000_000L; <span class="hljs-type">long</span> distanceToSun = <span class="hljs-number">149</span>_600_000L;</pre></div><p id="bf47">Note that the <b>L</b> character at the end of the number indicates that the value is a <b>long</b>.</p><h1 id="6d5d">float</h1><p id="11bb">The <b>float</b> data type is a single-precision 32-bit IEEE 754 floating point and is used to represent decimal numbers with a range of approximately <b>-3.4E38</b> to <b>3.4E38</b> and a precision of 6–7 digits. This data type is used for values that require less precision than what <b>double</b> provides or when memory is a concern.</p><p id="6c12">Example:</p><div id="7392"><pre><span class="hljs-built_in">float</span> weight = 65.5f; <span class="hljs-built_in">float</span> temperature = 25.7f;</pre></div><p id="2be2">Note that the <b>f</b> character at the end of the number indicates that the value is a <b>float</b>.</p><h1 id="134d">double</h1><p id="90b4">The <b>double</b> data type is a double-precision 64-bit IEEE 754 floating point and is used to represent decimal numbers with a higher precision than what <b>float</b> can provide. This data type is commonly used for scientific and mathematical calculations that require high accuracy.</p><p id="0b18">Example:</p><div id="b169"><pre><span class="hljs-type">double</span> pi = <span class="hljs-number">3.141592653589793</span>; <span class="hljs-type">double</span> e = <span class="hljs-number">2.718281828459045</span>;</pre></div><h1 id="5a6c">char</h1><p id="76a7">The <b>char</b> data type is a single 16-bit Unicode character and is used to represent a single character.</p><p id="9c8d">Example:</p><div id="cfa9"><pre><span class="hljs-type">char</span> firstLetter = <span class="hljs-string">'a'</span>; <span class="hljs-type">char</span> lastLetter = <span class="hljs-string">'z'</span>;</pre></div><p id="bd2a">It can also be used for numerical values between 0 and 65,535. It does not support negative values, making it effectively an unsigned data type.</p><div id="0fe1"><pre><span class="hljs-comment">// Parsing an unsigned short</span> <span class="hljs-built_in">short</span> s = (<span class="h

Options

ljs-built_in">short</span>) Integer.parseInt(<span class="hljs-string">"64000"</span>); System.<span class="hljs-keyword">out</span>.println(Short.toUnsignedInt(s)); <span class="hljs-comment">// 64000</span></pre></div><h1 id="a3b5">Default values</h1><p id="c9b3">In Java, if a primitive data type is declared as an instance variable, it will have a default value assigned to it automatically. The default values for each primitive data type are:</p><ul><li><code>boolean</code>: <code>false</code></li><li><code>byte</code>: <code>0</code></li><li><code>short</code>: <code>0</code></li><li><code>int</code>: <code>0</code></li><li><code>long</code>: <code>0L</code></li><li><code>float</code>: <code>0.0f</code></li><li><code>double</code>: <code>0.0d</code></li><li><code>char</code>: <code>\u0000</code> <i>(null character)</i></li></ul><p id="e758">It is not necessary to assign a default value to a primitive data type, as Java will do it automatically.</p><h1 id="1a49">Conclusion</h1><p id="f037">Choosing the right data type in Java is important for efficient memory usage and accurate calculations. In summary, use <b>boolean</b> for logical values, <b>byte</b> for small integer values, <b>short</b> for integer values with a larger range, <b>int</b> for integer values that do not require a large range or high.</p><div id="7822" class="link-block"> <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html"> <div> <div> <h2>Primitive Data Types</h2> <div><h3>The Java programming language is statically-typed, which means that all variables must first be declared before they…</h3></div> <div><p>docs.oracle.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div> </div> </div> </a> </div><h1 id="0774">Thanks for reading</h1><ul><li>👏<i> Please clap for the story (50 claps) to help the article to be spread</i></li><li>🌐 <i>Share the story on Social Media</i></li><li><i>More stories about <a href="https://medium.com/@wagnerjfr/lists">Programming, Career, AI and more</a>.</i></li><li>🔔<i> Follow me: <a href="https://medium.com/@wagnerjfr">Medium</a> | <a href="https://www.linkedin.com/in/wagner-franchin/">LinkedIn</a> | <a href="https://twitter.com/wagnerjfr">Twitter</a></i></li><li>📝 <i>Join the <a href="https://medium.com/@wagnerjfr/membership">Medium Membership Program</a> for only 5$ a month, and support me and other writers to keep on the tremedous work.</i></li></ul></article></body>

Java Primitive Data Types, What you need to know.

Cheatsheet: Sizes, Ranges, and Default Values

In Java, there are eight primitive data types that can be used to represent different kinds of data. In this article, we will discuss each of them and provide examples of when to use them.

boolean

The boolean data type is used to represent a logical value of either true or false. It is commonly used in conditional statements, loops, and other logical operations. Its size isn’t something that’s precisely defined

Example:

boolean isRaining = true;
boolean isSunny = false;

byte

The byte data type is an 8-bit signed two's complement integer and is used to represent whole numbers between -128 and 127. This data type is often used for small integer values, especially when working with streams of data.

Example:

byte myByte = 10;

short

The short data type is a 16-bit signed two's complement integer and is used to represent whole numbers between -32,768 and 32,767. This data type is used for integer values that require a larger range than what `byte` can provide but less than what `int` can provide.

Example:

short temperature = -20;

int

The int data type is a 32-bit signed two's complement integer and is used to represent whole numbers between -2,147,483,648 and 2,147,483,647. This data type is commonly used for integer values that do not require a large range or high precision. In Java, int is the default data type for integer values.

Example:

int age = 25;
int quantity = 10;

long

The long data type is a 64-bit two's complement integer and is used to represent whole numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. This data type is used for integer values that require a larger range than what `int` can provide. The long data type should be used when int is not enough to represent the required values.

Example:

long population = 7_594_000_000L;
long distanceToSun = 149_600_000L;

Note that the L character at the end of the number indicates that the value is a long.

float

The float data type is a single-precision 32-bit IEEE 754 floating point and is used to represent decimal numbers with a range of approximately -3.4E38 to 3.4E38 and a precision of 6–7 digits. This data type is used for values that require less precision than what double provides or when memory is a concern.

Example:

float weight = 65.5f;
float temperature = 25.7f;

Note that the f character at the end of the number indicates that the value is a float.

double

The double data type is a double-precision 64-bit IEEE 754 floating point and is used to represent decimal numbers with a higher precision than what float can provide. This data type is commonly used for scientific and mathematical calculations that require high accuracy.

Example:

double pi = 3.141592653589793;
double e = 2.718281828459045;

char

The char data type is a single 16-bit Unicode character and is used to represent a single character.

Example:

char firstLetter = 'a';
char lastLetter = 'z';

It can also be used for numerical values between 0 and 65,535. It does not support negative values, making it effectively an unsigned data type.

// Parsing an unsigned short
short s = (short) Integer.parseInt("64000");
System.out.println(Short.toUnsignedInt(s)); // 64000

Default values

In Java, if a primitive data type is declared as an instance variable, it will have a default value assigned to it automatically. The default values for each primitive data type are:

  • boolean: false
  • byte: 0
  • short: 0
  • int: 0
  • long: 0L
  • float: 0.0f
  • double: 0.0d
  • char: \u0000 (null character)

It is not necessary to assign a default value to a primitive data type, as Java will do it automatically.

Conclusion

Choosing the right data type in Java is important for efficient memory usage and accurate calculations. In summary, use boolean for logical values, byte for small integer values, short for integer values with a larger range, int for integer values that do not require a large range or high.

Thanks for reading

Programming
Java
Coding
Software Development
Guides And Tutorials
Recommended from ReadMedium