Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Primitive data types (also known as standard data types) are the data types that are built into the Java language. The Java compiler holds details instructions on each operation the data types that are built into the Java language. The Java compiler holds detailed instructions on each legal operation the data type supports. There are eight primitive data types in Java.
Data Type Byte Short Int Size/Form at (bits) Description Byte-length integer Short integer Integer Long integer Single precision floating Point Double precision floating Point A single character A boolean value (true or false) Range -128 to 128 (signed) 0 to 255 (unsigned) -215 to 215 -1 -231 to 231 -1 -263 to 263 -1 +/- about 1039 +/- about 10317
8 16 32 64 32 64 16 1
The data types byte, short, int, long, float and double are numeric data types. The first four of these can hold only whole numbers whereas the last two (float and double) can hold decimal values like 5.05. All these data types can hold negative values. However, the keyword unsigned can be used to restrict the range of values to positive numbers. Amongst others, Boolean can hold only the value true or false and char can hold only a single character. Derived Data Types Abstract data types are based on primitives data types and have more functionality that the primitive data types. For example, String is an abstract data type that can store alphabets, digits and other characters like /, (); :$#. You cannot perform calculations on a variable of the string data type even if the data stored in it has digits.
BC0047-JAVA PROGRAMMING => A Is-A relationship. => A Part-Of-relationship. => A Has-A relationship. Consider for a moment the similarities and differences among the following objects/classes: Automobile, Ford, Porsche, Car and Engine. We can make the following observations: => A truck is a kind of an automobile. => A car is a (different) kind of an automobile. => An engine is a part of an automobile. => An automobile has an engine. => The ford is a car. A-Kind-Of Relationship Taking the example of a human being and a elephant, both are kind-of mammals. As human beings and elephants are kind-of mammals, they share the attributes and behaviors of mammals. Human being and elephants are subset of the mammals class. The following figure depicts the relationship between the Mammals and Human Begin classes:
Is-A Relationship Lets take an instance of the human being class peter, who is a human being and, therefore, a mammal. The following figure depicts the is a relationship.
Has-A Relationship/Part-Of Relationship A human being has a heart. This represents a has-a relationship. The following figure depicts the relationship between a human being and a heart.
BC0047-JAVA PROGRAMMING
BC0047-JAVA PROGRAMMING
int data = inputstream.read(); You can case the returned int to a char like this: char aChar = (char) data; Subclasses of InputStream may have alternative read() methods. For instance, the DataInputStream allows you to read Java primitives like int, long, float, double, boolean etc. with its corresponding methods readBoolean(), redouble() etc. End of Stream If the read() method returns -1, the end of stream has been reached, meaning there is no more data to read in the InputStream. That is, -1 as int value, not -1 as byte or short value. There is a difference here! When the end of stream has been reached, you can close the InputStream. InputStreams and Sources An InputStream is typically always connected to some data source, like a file, network connection, pipe etc. This is also explained in more detail in the Java IO Overview text. OutputStream class The OutputStream class is the base class of all output streams in the Java IO API. Subclasses include the BufferedOutputStream and the FileOutputStream among others. To see a full list of streams, go to the bottom table of the Java IO Overview page. OutputStream's are used for writing byte based data, one byte at a time. Here is an example: OutputStream output = new FileOutputStream("c:\\data\\output-text.txt"); while(moreData) { int data = getMoreData(); output.write(data); } output.close(); The write() method of an OutputStream takes an int which contains the byte value of the byte to write. Subclasses of OutputStream may have alternative write() methods. For instance, the DataOutputStream allows you to write Java primitives like int, long, float, double, boolean etc. with its corresponding methods writeBoolean(), writeDouble() etc. OutputStream's and Destinations
BC0047-JAVA PROGRAMMING An Output Stream is typically always connected to some data destination, like a file, network connection, pipe etc. This is also explained in more detail in the Java IO Overview text.
Array Index Out Of Bounds Exception The exception Array Index Out Of Bounds Exception is thrown when an attempt is made to access an array element beyond the index of the array. For example, if you try to access the eleventh element of an array thats has only ten elements, the exception will be thrown.
BC0047-JAVA PROGRAMMING
5. Write a Java program to find the sum of 1+3+5+. ,for 10 terms in the series. Ans. import java.io*; class sum { public static void main(string arg[])throws IO Exception { InputStreamReader read=new InputStreamReader(System.in); BufferedReader in= new BufferReader(read); int i,n,s=0; System.out.println(enter the nth term: ); n=Integer.parseInt(in.readLine()); for(i=0;i<=n;i++) { if(i%2!=0) { s=s+I; } } System.out.println(Sum of the series= +s); } }
6. Write a program in Java to check whether a given year is leap year or not.
Ans. /*A leap year in the Gregorian calendar has an extra day for February. *A leap year has 366 days. * * Algorithm to find a leap year * ------------------------------* if year % 400 = 0, leap year * else if year % 100 = 0, not a leap year * else if year % 4 = 0, leap year * else, not a leap year */ public class LeapYearCalculator { public static void main(String args[]) { /*Give the year here which needs to be checked for leap year * (Assuming that year given here >= 1582 and corresponds to a year in Gregorian calendar) */ int year = 2000; //Flag to store the test result
BC0047-JAVA PROGRAMMING boolean isLeapYear = false; if(year % 400 == 0) { isLeapYear = true; } else if (year % 100 == 0) { isLeapYear = false; } else if(year % 4 == 0) { isLeapYear = true; } else { isLeapYear = false; } //Output the test result if(isLeapYear) { System.out.println("Year "+year+" is a Leap Year"); } else { System.out.println("Year "+year+" is not a Leap Year"); } } }