Jump to content

Into Java - Part III: Difference between revisions

From EDM2
Ak120 (talk | contribs)
No edit summary
Line 57: Line 57:
This time you can take it easy and take a closer look at the absolutely smallest parts to build with, that is data types. How do you manage data types and how do Java shows its strength at this area? At last you will examine how to extend your Java environment with Swing.
This time you can take it easy and take a closer look at the absolutely smallest parts to build with, that is data types. How do you manage data types and how do Java shows its strength at this area? At last you will examine how to extend your Java environment with Swing.


=='''Data Types'''==
==Data Types==
 
To the contrary of REXX and other similar languages Java is a ''strongly typed'' programming language. This way <tt>javac</tt> will not let you compile the code unless you specify which data type you are using, or wish to use. Hence
To the contrary of REXX and other similar languages Java is a ''strongly typed'' programming language. This way<code> javac </code>will not let you compile the code unless you specify which data type you are using, or wish to use. Hence
<code>
 
x = 3.141592653589793;
{| width="100%" cellpadding="15" bgcolor="#FFE682"
</code>
| nowrap="Nowrap" bgcolor="#FFE682" valign="Top" |
<font color="Maroon"><code>   x = 3.141592653589793;
</code></font>
|}


is not valid, unless you have first ''declared''<code> x </code>as double. As
is not valid, unless you have first ''declared'' <tt>x</tt> as double. As
 
<code>
{| width="100%" cellpadding="15" bgcolor="#FFE682"
double x;
| nowrap="Nowrap" bgcolor="#FFE682" valign="Top" |
<font color="Maroon"><code>   double x;
  x = 3.141592653589793;
  x = 3.141592653589793;
</code></font>
</code>
|}


or the shortcut
or the shortcut
 
<code>
{| width="100%" cellpadding="15" bgcolor="#FFE682"
double x = 3.141592653589793;
| nowrap="Nowrap" bgcolor="#FFE682" valign="Top" |
</code>
<font color="Maroon"><code>   double x = 3.141592653589793;
</code></font>
|}


You may ask why this strong typing is necessary, as some other languages does not require it. There are at least two benefits from it: First it will make your code less error prone as you really have to convince yourself at compile time that it should work, else the compiler will grumble at your mistakes. Second the strong typing will give you a way to differentiate methods from each other, even though using the same method name (we will explore that a few issues ahead).
You may ask why this strong typing is necessary, as some other languages does not require it. There are at least two benefits from it: First it will make your code less error prone as you really have to convince yourself at compile time that it should work, else the compiler will grumble at your mistakes. Second the strong typing will give you a way to differentiate methods from each other, even though using the same method name (we will explore that a few issues ahead).


Java uses eight ''primitive types'', of which six are number types, one is<code> char </code>that is used for characters, and the last one is the<code> boolean </code>type for true/false values. These primitive types are not to be confused with any class named equally, that is<code> boolean </code>IS NOT an equivalent to the class Boolean of the java.lang package (but of course they have some kind of connection and you will look at that later on). The primitive types are really down as close to machine level as you ever will come when using Java, and you don't need to bother if you don't like to.
Java uses eight ''primitive types'', of which six are number types, one is <tt>char</tt> that is used for characters, and the last one is the <tt>boolean</tt> type for true/false values. These primitive types are not to be confused with any class named equally, that is <tt>boolean</tt> IS NOT an equivalent to the class Boolean of the java.lang package (but of course they have some kind of connection and you will look at that later on). The primitive types are really down as close to machine level as you ever will come when using Java, and you don't need to bother if you don't like to.
 
Primitive types must always be declared as shown above, but you never need to use the<code> new </code>operator as you have to when declaring a new object from a class.
 
A significant strength to other programming languages is that Java is not dependent upon the target machine. You may remember that an<code> int </code>in e.g. C/C++ is not of the same size on a 16-bit Intel as it is on a 32-bit SPARC, nor is it of the same size on a 32-bit processor when using Windows 3.x as it is using Windows 95/NT. That sounds quite confusing to most people, but Java is not that bothersome.


===<code>'''boolean'''</code>===
Primitive types must always be declared as shown above, but you never need to use the <tt>new</tt> operator as you have to when declaring a new object from a class.


Java introduces a<code> boolean </code>type, that can be<code> true </code>or<code> false</code>. This data type is used in any logical test that Java supports, i.e.
A significant strength to other programming languages is that Java is not dependent upon the target machine. You may remember that an <tt>int</tt> in e.g. C/C++ is not of the same size on a 16-bit Intel as it is on a 32-bit SPARC, nor is it of the same size on a 32-bit processor when using Windows 3.x as it is using Windows 95/NT. That sounds quite confusing to most people, but Java is not that bothersome.


{| width="100%" cellpadding="15" bgcolor="#FFE682"
===boolean===
| nowrap="Nowrap" bgcolor="#FFE682" valign="Top" |
Java introduces a <tt>boolean</tt> type, that can be <tt>true</tt> or <tt>false</tt>. This data type is used in any logical test that Java supports, i.e.
<font color="Maroon"><code>   boolean isSetToTrue;    // variable declaration without instantiation
<code>
boolean isSetToTrue;    // variable declaration without instantiation
  isSetToTrue = getBooleanAnswerFromSomething();    // asks a method
  isSetToTrue = getBooleanAnswerFromSomething();    // asks a method
  </code></font>
  if (isSetToTrue) {
 
<font color="Maroon"><code>    if (isSetToTrue) {
  // your code here will be executed if isSetToTrue is ''true''
  // your code here will be executed if isSetToTrue is ''true''
  }
  }
</code></font>
</code>
|}


This way you never have to think about which return value is true or which is false, as you have to using e.g. C/C++. Further, a boolean can never be converted to anything else, it is a boolean, period.
This way you never have to think about which return value is true or which is false, as you have to using e.g. C/C++. Further, a boolean can never be converted to anything else, it is a boolean, period.


==='''char'''===
===char===
 
The <tt>char</tt> type is designed to handle the 2-byte [http://www.unicode.org/ Unicode] characters, that is a <tt>char</tt> is a 2-byte sized. Since ASCII/ANSI is a small subset of Unicode you never have to worry about this powerful feature unless you are heading for the international application market. Java for OS/2 of course supports Unicode if you like to use it, a somewhat bigger download though. However, you never have to worry about how a <tt>char</tt> looks like "under the hood", as you almost never have to know exactly what happens within the Java machinery.
The<code> char </code>type is designed to handle the 2-byte [http://www.unicode.org/ Unicode] characters, that is a<code> char </code>is a 2-byte sized. Since ASCII/ANSI is a small subset of Unicode you never have to worry about this powerful feature unless you are heading for the international application market. Java for OS/2 of course supports Unicode if you like to use it, a somewhat bigger download though. However, you never have to worry about how a<code> char </code>looks like "under the hood", as you almost never have to know exactly what happens within the Java machinery.


To use single characters you have to use single quotes
To use single characters you have to use single quotes
<code>
char test = 'q';
</code>


{| width="100%" cellpadding="15" bgcolor="#FFE682"
Since <tt>char</tt> is a primitive data type you may use it with arrays right out of the box. Hence
| nowrap="Nowrap" bgcolor="#FFE682" valign="Top" |
<code>
<font color="Maroon"><code>    char test = 'q';
char[] chArray = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'};
</code></font>
</code>
|}
 
Since<code> char </code>is a primitive data type you may use it with arrays right out of the box. Hence
 
{| width="100%" cellpadding="15" bgcolor="#FFE682"
| nowrap="Nowrap" bgcolor="#FFE682" valign="Top" |
<font color="Maroon"><code>   char[] chArray = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'};
</code></font>
|}


is twelve characters stored in an char array, you may access them one at a time. Compare that to the String
is twelve characters stored in an char array, you may access them one at a time. Compare that to the String
<code>
String hw = "Hello World!"
</code>


{| width="100%" cellpadding="15" bgcolor="#FFE682"
[[Image:java3a.gif]]that is one single string containing twelve characters. The figure to your right will show the difference. The upper object is <tt>chArray</tt> containing twelve chars, and you know exactly how it looks like. The lower object is the string <tt>hw</tt>, but on the contrary you don't know how it looks like inside, except that you can get the value "Hello World!" from it if you like to.
| nowrap="Nowrap" bgcolor="#FFE682" valign="Top" |
<font color="Maroon"><code>    String hw = "Hello World!"
</code></font>
|}
 
[[Image:java3a.gif]]that is one single string containing twelve characters. The figure to your right will show the difference. The upper object is<code> chArray </code>containing twelve chars, and you know exactly how it looks like. The lower object is the string<code> hw</code>, but on the contrary you don't know how it looks like inside, except that you can get the value "Hello World!" from it if you like to.
 
==='''Number Data Types'''===


===Number Data Types===
Numbers can be divided into two groups, with decimals or without decimals, that is floating-point types vs. integers. Java uses two floating-point types, and it uses four integer types. Why?
Numbers can be divided into two groups, with decimals or without decimals, that is floating-point types vs. integers. Java uses two floating-point types, and it uses four integer types. Why?


Regarding floating-point numbers you will mainly use<code> double </code>that gives you maximum precision, but once upon a decade you may choose<code> float </code>that is processed faster or maybe when you run out of disk space.
Regarding floating-point numbers you will mainly use <tt>double</tt> that gives you maximum precision, but once upon a decade you may choose <tt>float</tt> that is processed faster or maybe when you run out of disk space.
 
Normally you will use the<code> int </code>integer. But often you must choose<code> long </code>when calling certain methods within the Java classes, or if you like to count the distance to the sun in meters. The other two integer data types,<code> short </code>and<code> byte</code>, are used in low-level file handling, or perhaps if you are managing very large amounts of integers.


Normally you will use the<code> int </code>integer. But often you must choose <tt>long</tt> when calling certain methods within the Java classes, or if you like to count the distance to the sun in meters. The other two integer data types, <tt>short</tt> and <tt>byte</tt>, are used in low-level file handling, or perhaps if you are managing very large amounts of integers.
{| width="100%" border="1" cellpadding="4"
{| width="100%" border="1" cellpadding="4"
|- bgcolor="#FFE682"
|- bgcolor="#FFE682"
| valign="Top" | <font color="Maroon">'''Type'''</font>
! valign="Top" | <font color="Maroon">Type</font>
| <font color="Maroon">'''Size'''</font>
! <font color="Maroon">Size</font>
| <font color="Maroon">'''Range''' (approximately)</font>
! <font color="Maroon">Range (approximately)</font>
| <font color="Maroon">'''Code example'''</font>
! <font color="Maroon">Code example</font>
|- bgcolor="#FFE682"
|- bgcolor="#FFE682"
| colspan="4" | <font color="Maroon">''Floating-point numbers''</font>
| colspan="4" | <font color="Maroon">''Floating-point numbers''</font>
Line 168: Line 139:
| <code>float f = 3.14159F;</code>
| <code>float f = 3.14159F;</code>
|- bgcolor="#FFE682"
|- bgcolor="#FFE682"
| colspan="4" | <font color="Maroon">''Integer numbers''</
| colspan="4" | <font color="Maroon">''Integer numbers''
 
|}


[[Category:Languages Articles]]
[[Category:Languages Articles]]

Revision as of 22:05, 14 September 2016

By Simon Gronlund

Part I

Part II

Part III

Part IV

Part V

Part VI

Part VII

Part VIII

Part IX

Part X

Part XI

Part XII

Part XIII

Part IV

Part XV

Part XVI

Part XVII

Part XVIII

Part XIX

Part XX

Part XXI

Part XXII

Part XXIII

Welcome back to the Into Java column. The past month you have acquainted yourself with the Java API documentation, as well as you have taken a glance at the theory lesson and now you know the basics about Java. You know how to write some Java code, to save the app, and to compile and run the application from your command line.

This time you can take it easy and take a closer look at the absolutely smallest parts to build with, that is data types. How do you manage data types and how do Java shows its strength at this area? At last you will examine how to extend your Java environment with Swing.

Data Types

To the contrary of REXX and other similar languages Java is a strongly typed programming language. This way javac will not let you compile the code unless you specify which data type you are using, or wish to use. Hence

x = 3.141592653589793;

is not valid, unless you have first declared x as double. As

double x;
x = 3.141592653589793;

or the shortcut

double x = 3.141592653589793;

You may ask why this strong typing is necessary, as some other languages does not require it. There are at least two benefits from it: First it will make your code less error prone as you really have to convince yourself at compile time that it should work, else the compiler will grumble at your mistakes. Second the strong typing will give you a way to differentiate methods from each other, even though using the same method name (we will explore that a few issues ahead).

Java uses eight primitive types, of which six are number types, one is char that is used for characters, and the last one is the boolean type for true/false values. These primitive types are not to be confused with any class named equally, that is boolean IS NOT an equivalent to the class Boolean of the java.lang package (but of course they have some kind of connection and you will look at that later on). The primitive types are really down as close to machine level as you ever will come when using Java, and you don't need to bother if you don't like to.

Primitive types must always be declared as shown above, but you never need to use the new operator as you have to when declaring a new object from a class.

A significant strength to other programming languages is that Java is not dependent upon the target machine. You may remember that an int in e.g. C/C++ is not of the same size on a 16-bit Intel as it is on a 32-bit SPARC, nor is it of the same size on a 32-bit processor when using Windows 3.x as it is using Windows 95/NT. That sounds quite confusing to most people, but Java is not that bothersome.

boolean

Java introduces a boolean type, that can be true or false. This data type is used in any logical test that Java supports, i.e.

boolean isSetToTrue;    // variable declaration without instantiation
isSetToTrue = getBooleanAnswerFromSomething();    // asks a method
if (isSetToTrue) {
// your code here will be executed if isSetToTrue is true
}

This way you never have to think about which return value is true or which is false, as you have to using e.g. C/C++. Further, a boolean can never be converted to anything else, it is a boolean, period.

char

The char type is designed to handle the 2-byte Unicode characters, that is a char is a 2-byte sized. Since ASCII/ANSI is a small subset of Unicode you never have to worry about this powerful feature unless you are heading for the international application market. Java for OS/2 of course supports Unicode if you like to use it, a somewhat bigger download though. However, you never have to worry about how a char looks like "under the hood", as you almost never have to know exactly what happens within the Java machinery.

To use single characters you have to use single quotes

char test = 'q';

Since char is a primitive data type you may use it with arrays right out of the box. Hence

char[] chArray = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'};

is twelve characters stored in an char array, you may access them one at a time. Compare that to the String

String hw = "Hello World!"

that is one single string containing twelve characters. The figure to your right will show the difference. The upper object is chArray containing twelve chars, and you know exactly how it looks like. The lower object is the string hw, but on the contrary you don't know how it looks like inside, except that you can get the value "Hello World!" from it if you like to.

Number Data Types

Numbers can be divided into two groups, with decimals or without decimals, that is floating-point types vs. integers. Java uses two floating-point types, and it uses four integer types. Why?

Regarding floating-point numbers you will mainly use double that gives you maximum precision, but once upon a decade you may choose float that is processed faster or maybe when you run out of disk space.

Normally you will use the int integer. But often you must choose long when calling certain methods within the Java classes, or if you like to count the distance to the sun in meters. The other two integer data types, short and byte, are used in low-level file handling, or perhaps if you are managing very large amounts of integers.

Type Size Range (approximately) Code example
Floating-point numbers
double 8 bytes 1.7977E+308, 15 significant digits double d = 3.14159;
float 4 bytes 3.4028E+38, 6 significant digits float f = 3.14159F;
Integer numbers