1 / 115

JAVA Data Types and Simple Program

JAVA Data Types and Simple Program. By Balaji.VP 9791118054 vpbala2000@gmail.com. Simple Program. Import java.lang.math; Class squareroot { public static void main (String args[]) { double x = 5; double y; y = Math.sqrt (x);

tomas
Télécharger la présentation

JAVA Data Types and Simple Program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAVA Data Types and Simple Program By Balaji.VP 9791118054 vpbala2000@gmail.com

  2. Simple Program Import java.lang.math; Class squareroot { public static void main (String args[]) { double x = 5; double y; y = Math.sqrt (x); System.out.println (“The Value of Y =“ +y); } }

  3. Explanation of the Program • The first lie is “class squareroot” • It is Object –Oriented Construct. Nothing but everything must be placed inside the class. • The word class is a keyword. It is used for create a new class. • Here “squareroot” is identifier. • Next is braces ( This is similar to C++) • The third line is public static void main (String args [])

  4. Explanation of the Program • Public: The keyword public is an access specifiers that declares the main method as unprotected and therefore making it accessible to all other classes. Static: Which declares this method as one that belongs to the entire class and not a part of the class any objects of the class. The main must always declare as static since the interpreter uses this method before any objects are created. Void: The type modifier void states that the main method does not return any value.

  5. Second Program Import java.lang.*; Class hall { float len; float bre; void getdata (float a, float b) { len = a; bre = b; } }

  6. Second Program Class area { public static void main (string args []) { float area; hall hall1 = new hall (); hall1.getdata (5,10); area = hall1.len * hall1.bre; System.out.println (“Area =“ + area) } }

  7. JAVA Tokens • Most statements contain expressions, which describe the actions carried out the data. Smallest individual units in a program are known as Tokens. • Reserved Keywords. (60 Keywords, false, null, true, package etc.,) • Identifiers (alphabets, digits and underscore and dollar sign characters.) • Literals. (Integer, Floating Point. Character, String, Boolean) • Operators. • Separators.

  8. JAVA Tokens (Separators) Parentheses ()  Method definition and invocation, defining precedence in expressions. Braces {}  Define a block of code for classes, methods and local scopes. Brackets []  Declare array types and for dereferencing array values. Semicolon ;  Separate the statements. Comma ,  Consecutive identifiers in a variable declaration and chain statements (For loop and int a,b) Period .  Separate package names from the sub-packages and class, separate a variable or method from a reference variable.

  9. Constants • JAVA Constants Numeric Constants Character Constants Integer Cons Real Cons Char Cons String Cons 123, 0.0085, -0.75, ‘X’, ‘5’ “Hello”

  10. Escapes Sequences • ‘\b’ --- > Back Space. • ‘\f’ --- > Form Feed. • ‘\n’ --- > New line. • ‘\t’ --- > Horizontal Tab.

  11. Data Types • Data types are two types Primitive and Non- Primitive. • Primitive Non- Primitive • Numeric Non-Numeric Class, Arrays, Interface. Integer, Float Char, Boolean

  12. Size • Integer Type Size Byte 1 (Byte) Short 2 Int 4 Long 8 Floating Point: Float 4 Double 8

  13. Declaration and Giving Values • Int a; • Float a,b; • Double s; • Byte b; • Char c1,c2,c3; • Int a = 10; • Float x = 20.36; • Char c1 = ‘a’

  14. Reading data from Keyboard Import java.lang.*; Import java.io.DataInputStream; Class read { public static void main (String args []) { DataInputStream in = new DataInputStream (System. in) int num = 0; float num1 = 0.0f;

  15. Try { System.out.println (“Enter a number :”); num = Integer.parseInt (in.readLine () ); System.out.println (“Enter a Next Number:”); num1 = Float.valueOf(in.readLine() ).floatValue (); } Catch (Exception e) { }

  16. System.out.println (“Num = “ +num); System.out.println (“Num1 = “ +num 1); } } Output: Enter a Number: 123 Enter the Next Number: 123.45 Num = 123, Num1 = 1234.45

  17. Scope of the Variables • Java variables are classified in three types, 1. Instance Variables. 2. Class Variables. 3. Local Variables. The instance and class variable are declared inside a class. Instance variable are created when the objects are instantiated and therefore they are associated with the objects. They take different value for each object. Variables declared and used inside the methods are called local variables.

  18. Symbolic Constants • Symbolic names take the same form as variable names. But they are written in CAPITALS to visually distinguish them from normal variable names. • After declaration of symbolic constants, they should not be assigned any other value within the program by using an assignment statement. • Like final int PEOPLE = 100; we can not change the value. • This is not done in C and C++ where it is defined using the #define statement. They can NOT be declared inside a method. They should be used only as class data members in the beginning of the class.

  19. Arithmetic and Relational Operators • 1. a – b 10. == is equal to • 2. a + b 11. != is not equal to • 3. a * b • 4. a / b • 5. a % b • 6. < is less than • 7. <= is less than or equal to • 8. > is greater than • 9. >= is greater than or equal to

  20. Logical Operators • && logical AND • || logical OR • ! logical NOT • Ex: if (age > 55 && salary < 1000) if (number <0 || number >1000)

  21. Assignment Operators • a = a+1 • a = a -1 • a = a* (n +1) • a = a / (n+1) • a = a % b • y = ++m s not equal to y = m++ Ex: m = 5; but y = m++ means, y = 5 but m = 6 y = ++m y = 6

  22. Conditional Operator • Exp1 ? Exp2 : Exp3 Ex: a =10; b = 15; x = (a>b) ? a : b In terms of if statement if (a>b) x = a; else x = b;

  23. Bitwise Operator • These operators are used for testing the bits, or shifting them to right to left. • It is may not ne applies to float and double. • & bitwise AND • ! Bitwise OR • ^ Bitwise exclusive OR • ~ One’s Complement • << shift left • >> shift right

  24. Special Operator • There are two special operator is there, • 1. instanceof • 2. selection operator (.) Instanceof: person instanceof student If it is true the object person belongs to the class student; otherwise it is false. Dot Operator: Person1.age // Reference to the variable age. Person 1.salary () // Reference to the method salary()

  25. If statement import java.lang.*; class if { public static void main (String args[]) { int I, count, count1, count2; float [] weight = { 45.10, 55.23, 47.25, 51.23, 54.23} float [] height = { 176.23, 174.56, 168.23, 170.89, 168. 52}

  26. count = 0; count 1 = 0; count2 = 0; For (i =0; i <=4; i++) { if (weight[i] < 50.0 && height [i] > 170.0) { count1 = count1 + 1; }

  27. count = count + 1; //Total Persons } count2 = count – count1; System.out.println (“Number of persons with..”); System.out.println (“Weight <50 and height > 170 =“ +count1); System.out.println (“Others =“ +count2); } }

  28. Else If Ladder class ladder { public static void main (String args[]) { int rollNumber [] = {111, 222, 333, 444}; int marks [] = {81, 75, 43, 58} for (int i =0; i <rollNumber. length; i++) { if (marks [i] >79)

  29. System.out.println (rollNumber [i] + “Honors”); else if (marks [i] > 59) System.out.println (rollNumber [i] + “I Division”); else if (marks [i] > 49 System.out.println (rollNumber [i] + “II Division”); else System.out.println (rollNumber [i] + “Fail ”); } } }

  30. Switch case class guide { public static void main (String args []) { char ch; System.out.println (“Select your Choice”); System.out.println (“ M  Madras”); System.out.println (“ B  Bombay”); System.out.println (“ C  Calcutta”);

  31. System.out.println (“ Please select any one”); try { switch (ch = (char)System.in.read() ) { case ‘M’: case ‘m’: System.out.println (“ Madras  Booklet 5”); break;

  32. case ‘b’: case ‘B’: System.out.println (“ Bombay  Booklet7”); break; case ‘C’ case ‘c’ System.out.println (“ Calcutta  Booklet 10”); break; default: System.out.println (“ Invalid Choice”);

  33. } } catch (Exception e ) { System.out.println (“I/O Error”); } } }

  34. While Loop class while { public static void main (String args []) { StringBuffer string = new StringBuffer (); char c; System.out.println (“Enter a string”); try {

  35. while ( ( c = (char)System.in.read() ) != ‘\n’) { string.append (c); } } Catch (Exception e) { System.out.println (“Error in input”); }

  36. System.out.println (“You have entered”); System.out.println (string); } } Output: Enter a string I am a good boy You have entered I am a good boy

  37. Do-while class do { public static void main (String args []) { int row, column, y; System.out.println (“Multiplication Table \n”); row = 1; do { column = 1;

  38. do { y = row * column; System.out.println (“ “ +y); column = column + 1; } while (column <=3) { System.out.println (“\n”); row = row + 1; }

  39. } while (row <=3) } } Output: 1 2 3 2 4 6 3 6 9

  40. class for { public static void main (String args []) { long p; int n; double q; System.out.println (“ 2 to power –n n 2 to power n’); p = 1;

  41. for (n =0; n<10; ++n) { if (n == 0) p = 1; else p = p * 2; q = 1.0/ (double) p; System.out.println (“ “ + q + “ “ +n “ “ +p); } } }

  42. Output: 2 to power –n n 2to power n 1 0 1 o.5 1 2 0.25 3 4 0.00195313 9 512

  43. Class, Objects and Methods • Java Program must be encapsulated in a class that defines the state and behavior of the basic program components knows as “Objects” • In JAVA Program the data items are called fields and the functions are called methods. • “ class is a user defined data type with a template that serves to define its properties. Once the class type has been defined, we can create “variables” of that type using declarations that are similar to the basic type declarations.

  44. Define Class • class classname [extends supercalssname] { [ variable declarations] [ method declarations] } Adding Variables: Data is encapsulated in a class by placing data field inside the body of the class definition. These variables are called ‘instance variable’. They are created whenever an object of the class is instantiated.

  45. Adding Methods Type method name ( parameter – list) { method- body; } Basic idea of method declaration: The name of the method. The type of the value the method return. A list of parameters. The body of the method.

  46. Example class rectangle { int length, width; void getdata (int x, int y) { length = x; width = y; }

  47. int rectArea () { int area = length * width; return (area); }

  48. Creating Objects • Objects in Java are created using the new operator. The new operator created an object of the specified class and returns a reference to that object. Ex: rectangle rect; rect = new rectangle (); or rectangle rect = new rectangle ();

  49. Accessing Class Members • Objectname.varibale name; • Objectname.methodname (parameter – list) Ex: rect.length = 15; rect.width = 10; Calling Methods: rect. getdata (10,15);

  50. The first one is, to access the instance variable using the dot operator and compute the area. int area = rect.length * rect.width; The second one is, to call the method area declared inside the class, That is int area = rect1.area();

More Related