BILKENT UNIVERSITY
CS533 Information Retrieval Systems
Technical Presentation Web Site
Barýþ UZ
 
Index of
this page
Introduction
Push Technologies
Channels
CGI
ASP
Java
Streaming Media
 
Overview
 Classes
Main Data Structures in Java
Object Oriented Programming
More about Object Oriented Programming
Applets
GUI
Images and audio
Multithreaded Programming
Links
 
Java supports most common data types in conventional programming languages like integer, character, floating point, boolean, etc. However, Java specifies everything and nothing is left to the compiler. For example, in C and C++, the size of int is machine dependent which is never the case in Java. In addition, casting between arbitrary variables are not allowed. You can only cast between numeric values and between super and subclasses of the same object. All numeric values are signed in Java.  

boolean, byte, short, int, long, float, double, char are allowed types and they have 1-bit, 1 byte, 2 bytes, 4 bytes, 8 bytes (two's complement), 4 bytes (IEEE 754), 8 bytes (IEEE 754) and 2 bytes. Only char and boolean types are unsigned in Java. char is not the same as byte, int, short or Strings. 

There is no restriction on the length of variable names. However, you'd better to use understandable and meaningful names for variables. 

Although Java handles the conversion between numeric values, it is better to explicitly warn the compiler to make casting. In my opinion, you should always explicitly define the type of result. Although Java does its own truncation and/or rounding, this may yield in the loss of precision in one of your trials. Especially when the results are too big or too small enough to store in the resulting variable, Java will possibly do it wrong. 

It is possible to convert strings to integers or vice versa easily in Java. toString() and valueOf() methods performs this job. 

    int i = Integer.valueOf("22").intValue(); // assigns the number 22 to integer i 
    double d = Double.valueOf("3.14").doubleValue(); // assigns the number 3.14 to double d
Declaring Arrays 

Since array is an indexed collection of similar type of data, we should first specify its data type. Global array declaration is as follows: 

    int[] k = new int[10];
creates an array of 10 integers. Index of array starts from 0 to 9 in this particular example. 

For multidimensional arrays, we use: 

    int[][] table = new int[10][15];
which defines a table of 150 elements; 10 rows and 15 columns. We can also initialize an array by using initial values: 
    int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} };
which defines a 4x3 matrix. 
 
 
PREVIOUS TOPIC
UP
(First page of this topic)
NEXT TOPIC
PREVIOUS PAGE 
(of this topic)
NEXT PAGE 
(of this topic)