Java in Easy Ways

Download Java in Easy Ways

Post on 08-Apr-2018

218 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

<ul><li><p>8/6/2019 Java in Easy Ways</p><p> 1/73</p><p>JJAAVVAA IINN EEAASSYY WWAAYYSS</p><p>Compilation : Randjithkumar, B.TechPage 1</p><p>Java in Easy</p><p>Ways</p></li><li><p>8/6/2019 Java in Easy Ways</p><p> 2/73</p><p>JJAAVVAA IINN EEAASSYY WWAAYYSS</p><p>Compilation : Randjithkumar, B.TechPage 2</p><p>JAVA AN INTRODUCTION</p><p>INTRODUCTION TO JAVA</p><p>Java is a simple and yet powerful object oriented programming language and it is in</p><p>many respects similar to C++. Java was given birth at Sun Microsystems, Inc. in 1991. Java was</p><p>conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at</p><p>Sun Microsystems, Inc. It was developed to provide a platform-independent programming</p><p>language.</p><p>Java (with a capital J) is a high-level, third generation programming language, like C, Fortran,</p><p>Smalltalk, Perl, and many others. You can use Java to write computer applications that crunch</p><p>numbers, process words, play games, store data or do any of the thousands of other things</p><p>computer software can do.</p><p>Platform independent</p><p>Unlike many other programming languages including C and C++ when Java is compiled, it is not</p><p>compiled into platform specific machine, rather into platform independent byte code. This byte</p><p>code is distributed over the web and interpreted by Java virtual Machine (JVM) on whichever</p><p>platform it is being run.</p><p>Compared to other programming languages, Java is most similar to C. However although Java</p><p>shares much of C's syntax, it is not C. Knowing how to program in C or, better yet, C++, will</p><p>certainly help you to learn Java more quickly, but you don't need to know C to learn Java. Unlike</p><p>C++ Java is not a superset of C. A Java compiler won't compile C code, and most large C</p><p>programs need to be changed substantially before they can become Java programs.</p><p>Java Virtual Machine</p><p>Java was designed with a concept of write once and run everywhere. Java Virtual Machine</p><p>plays the central role in this concept. The Java Virtual Machine (JVM) is the environment in</p><p>which Java programs execute. It is a software that is implemented on top of real hardware and</p><p>operating system. When Java source code (.java files) is compiled, it is translated into Java</p></li><li><p>8/6/2019 Java in Easy Ways</p><p> 3/73</p><p>JJAAVVAA IINN EEAASSYY WWAAYYSS</p><p>Compilation : Randjithkumar, B.TechPage 3</p><p> bytecodes and then placed into (.class) files. The JVM executes Java bytecodes. So Java</p><p>bytecodes can be thought of as the machine language of the JVM. A Java virtual machine can</p><p>either interpret the bytecode one instruction at a time or the bytecode can be compiled further for</p><p>the real microprocessor using what is called a just-in-time compiler. The JVM must beimplemented on a particular platform before compiled Java programs can run on that platform.</p><p>Java has powerful features. The following are some of them:-</p><p>Java is object oriented</p><p>Since Java is an object oriented programming language it has following advantages:</p><p>y Reusability of Codey Emphasis on data rather than procedurey Data is hidden and cannot be accessed by external functionsy Objects can communicate with each other through functionsy New data and functions can be easily added</p><p>Java is Distributed</p><p>With extensive set of routines to handle TCP/IP protocols like HTTP and FTP java can open and</p><p>access the objects across net via URLs.</p><p>Java is Multithreaded</p><p>One of the powerful aspects of the Java language is that it allows multiple threads of execution to</p><p>run concurrently within the same program A single Java program can have many different</p><p>threads executing independently and continuously. Multiple Java applets can run on the browser</p><p>at the same time sharing the CPU time.</p><p>Java is Secure</p></li><li><p>8/6/2019 Java in Easy Ways</p><p> 4/73</p><p>JJAAVVAA IINN EEAASSYY WWAAYYSS</p><p>Compilation : Randjithkumar, B.TechPage 4</p><p>Java was designed to allow secure execution of code across network. To make Java secure many</p><p>of the features of C and C++ were eliminated. Java does not use Pointers. Java programs cannot</p><p>access arbitrary addresses in memory.</p><p>Garbage collection</p><p>Automatic garbage collection is another great feature of Java with which it prevents inadvertent</p><p>corruption of memory. Similar to C++, Java has a new operator to allocate memory on the heap</p><p>for a new object. But it does not use delete operator to free the memory as it is done in C++ to</p><p>free the memory if the object is no longer needed. It is done automatically with garbage</p><p>collector.</p><p>Application of Java</p><p>Java has evolved from a simple language providing interactive dynamic content for web pages to</p><p>a predominant enterprise-enabled programming language suitable for developing significant and</p><p>critical applications. Today, Java is used for many types of applications including Web based</p><p>applications, Financial applications, Gaming applications, embedded systems, Distributed</p><p>enterprise applications, mobile applications, Image processors, desktop applications and many</p><p>more.</p><p>The most special about Java in relation to other programming languages is that it lets you write</p><p>special programs called applets that can be downloaded from the Internet and played safely</p><p>within a web browser. Traditional computer programs have far too much access to your system</p><p>to be downloaded and executed willy-nilly. Although you generally trust the maintainers of</p><p>various ftp archives and bulletin boards to do basic virus checking and not to post destructive</p><p>software, a lot still slips through the cracks. Even more dangerous software would be</p><p>promulgated if any web page you visited could run programs on your system.</p><p>JAVA VS. C++</p><p>Java C++</p></li><li><p>8/6/2019 Java in Easy Ways</p><p> 5/73</p><p>JJAAVVAA IINN EEAASSYY WWAAYYSS</p><p>Compilation : Randjithkumar, B.TechPage 5</p><p>Method bodies must be defined inside the class</p><p>to which they belong.</p><p>Method bodies must be defined inside the class</p><p>to which they belong.</p><p> No forward referencing required. Explicit forward referencing required.</p><p> No preprocessor. Heavy reliance on preprocessor.</p><p> No comma operator. Comma operator.</p><p> No variable-length parameter lists. Variable-length parameter lists.</p><p>No optional method parameters. Optional function parameters.</p><p>No const reference parameters. const reference parameters.</p><p>No goto Goto</p><p>Labels on break and continue. No labels on break and continue.</p><p>Command-line arguments do not include the</p><p>program name.</p><p>Command-line arguments do not include the</p><p>program name.</p><p>Main method cannot return a value. Main function can return a value.</p><p> No global variables. Global variables.</p><p>Character escape sequences can appear in a</p><p>program.</p><p>Character escape sequences must appear in astring</p><p>or character literal.</p><p>Cannot mask identifiers through scope. Can mask identifiers through scope.</p><p>DATA TYPES IN JAVA</p><p>Data type defines a set of permitted values on which the legal operations can beperformed. In java, all the variables needs to be declared first i.e. before using a particular</p><p>variable, it must be declared in the program for the memory allocation process. Like</p><p>int pedal = 1;</p><p>This statement exists a field named "pedal" that holds the numerical value as 1. The value</p><p>assigned to a variable determines its data type, on which the legal operations of java are performed. This behavior specifies that, Java is a strongly-typed programming language.</p><p>The data types in the Java programming language are divided into two categories and can be</p><p>explained using the following hierarchy structure :</p></li><li><p>8/6/2019 Java in Easy Ways</p><p> 6/73</p><p>JJAAVVAA IINN EEAASSYY WWAAYYSS</p><p>Compilation : Randjithkumar, B.TechPage 6</p><p>Primitive Data Types</p><p>The primitive data types are predefined data types, which always hold the value of the same</p><p>data type, and the values of a primitive data type don't share the state with other primitive</p><p>values. These data types are named by a reserved keyword in Java programming language.</p><p>There are eight primitive data types supported by Java programming language :</p><p>byteThe byte data type is an 8-bit signed two's complement integer. It ranges from -128 to127(inclusive). This type of data type is useful to save memory in large arrays.. We can also use</p><p>byte instead ofint to increase the limit of the code. The syntax of declaring a byte type variableis shown as:</p><p>byte b = 5;</p><p>shortThe short data type is a 16-bit signed two's complement integer. It ranges from -32,768 to</p><p>32,767. short is used to save memory in large arrays. The syntax of declaring a short typevariable is shown as:</p><p>short s = 2;</p><p>intThe int data type is used to store the integer values not the fraction values. It is a 32-bit signed</p><p>two's complement integer data type. It ranges from -2,147,483,648 to 2,147,483,647 that is more</p><p>enough to store large number in your program. However for wider range of values uselong. Thesyntax of declaring a int type variable is shown as:</p><p>int num =50;</p><p>longThe long data type is a 64-bit signed two's complement integer. It ranges from -</p></li><li><p>8/6/2019 Java in Easy Ways</p><p> 7/73</p><p>JJAAVVAA IINN EEAASSYY WWAAYYSS</p><p>Compilation : Randjithkumar, B.TechPage 7</p><p>9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use this data type with larger rangeof values. The syntax of declaring a long type variable is shown as:</p><p>long ln = 746;</p><p>floatThe float data type is a single-precision 32-bit IEEE 754 floating point. It ranges from1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative). Use a float</p><p>(instead of double) to save memory in large arrays. We do not use this data type for the exactvalues such as currency. For that we have to use java.math.BigDecimal class. The syntax of</p><p>declaring a float type variable is:</p><p>float f = 105.65;</p><p>float f = -5000.12;</p><p>double</p><p>This data type is a double-precision 64-bit IEEE 754 floating point. It ranges from4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative). This datatype is generally the default choice for decimal values. The syntax of declaring a double type</p><p>variable is shown as:</p><p>double d = 6677.60;</p><p>charThe char data type is a single 16-bit, unsigned Unicode character. It ranges from 0 to 65,535.They are not integral data type like int, short etc. i.e. the char data type can't hold the numeric</p><p>values. The syntax of declaring a char type variable is shown as:</p><p>char caps = 'c';</p><p>boolean</p><p>The boolean data type represents only two values: true and false and occupy is 1-bit in the</p><p>memory. These values are keywords in Java and represents the two boolean states: on oroff,</p><p>yes orno. We use boolean data type for specifying conditional statements as if, while, do, for.In Java, true and false are not the same as True and False. They are defined constants of the</p><p>language. The syntax of declaring a boolean type variable is shown as:</p><p>boolean result = true;</p><p>Integer Data Types</p><p>An integer number can hold a whole number. Java provides four different primitive integer datatypes that can be defined as byte, short, int, and long that can store both positive and negative</p><p>values. The ranges of these data types can be described using the following table:</p></li><li><p>8/6/2019 Java in Easy Ways</p><p> 8/73</p><p>JJAAVVAA IINN EEAASSYY WWAAYYSS</p><p>Compilation : Randjithkumar, B.TechPage 8</p><p>Data Type Size (in bits) Minimum Range Maximum Range</p><p>ByteOccupy 8 bits</p><p>in memory-128 +127</p><p>ShortOccupy 16 bitsin memory</p><p>-32768 +32767</p><p>IntOccupy 32 bitsin memory</p><p>-2147483648 +2147483647</p><p>LongOccupy 64 bits</p><p>in memory-9223372036854775808 +9223372036854775807</p><p>Examples of floating-point literals are:</p><p>0</p><p>1123</p><p>-42000</p><p>Floating-point numbers</p><p>A floating-point number represents a real number that may have a fractional values i.e. In the</p><p>floating type of variable, you can assign the numbers in an in a decimal or scientific notation.Floating-point number have only a limited number of digits, where most values can be</p><p>represented only approximately. The floating-point types are float and double with a single- precision 32-bit IEEE 754 floating point and double-precision 64-bit IEEE 754 floating point</p><p>respectively. Examples of floating-point literals are:</p><p>10.0003</p><p>48.9-2000.15</p><p>7.04e12</p></li><li><p>8/6/2019 Java in Easy Ways</p><p> 9/73</p><p>JJAAVVAA IINN EEAASSYY WWAAYYSS</p><p>Compilation : Randjithkumar, B.TechPage 9</p><p>Reference Data Types</p><p>In Java a reference data type is a variable that can contain the reference or an address of</p><p>dynamically created object. These type of data type are not predefined like primitive data type.The reference data types are arrays, classes and interfaces that are made and handle according</p><p>to a programmer in a java program which can hold the three kind of values as:</p><p>array type</p><p>// Points to an array instance</p><p>class type</p><p>// Points to an object or a class instance</p><p>interface type// Points to an object and a method, which is</p><p>implemented to the corresponding interface</p><p>Interface Type:</p><p>Java provides an another kind of reference data type or a mechanism to support multipleinheritance feature called an interface. The name of an interface can be used to specify the type</p><p>of a reference. A value is not allowed to be assign to a variable declared using an interface typeuntil the object implements the specified interface.</p><p>When a class declaration implements an interface, that class inherits all of the variables andmethods declared in that interface. So the implementations for all of the methods declared in the</p><p>interface must be provided by that class. For example, Java provides an interface called</p><p>ActionListener whose method named actionPerformed() is used to handle the different kind of</p><p>event . Java also provides a class called Thread that implements Runnable interface.Thus the following assignment can be allowed:</p><p>Runnable r;</p><p>r = new Thread();</p></li><li><p>8/6/2019 Java in Easy Ways</p><p> 10/73</p><p>JJAAVVAA IINN EEAASSYY WWAAYYSS</p><p>Compilation : Randjithkumar, B.TechPage 10</p><p>OPERATORS IN JAVA</p><p>Java provides a rich operator environment. Most of its operators can be divided</p><p>into the following four groups: arithmetic, bitwise, relational, and logical. Java also</p><p>defines some additional operators that handle certain special situations.</p><p>The Simple Assignment Operator</p><p>One of the most common operators that encounter is the simple assignment operator "=". it</p><p>assigns the value on its right to the operand on its left:</p><p>int cadence = 0;int speed = 0;</p><p>int gear = 1;</p><p>This operator can also be used on objects to assign object references</p><p>The Arithmetic Operators</p><p>The Java programming language provides operators that perform addition, subtraction,</p><p>multiplication, and division. There's a good chance you'll recognize them by their counterparts in basic mathematics. The only symbol that might look new to you is "%", which divides one</p><p>operand by another and returns the remainder as its result. OperatorResult</p><p>+ Addition Subtraction (also unary minus)</p><p>* Multiplication/ Division</p><p>% Modulus++ Increment</p><p>+= Addition assignment= Subtraction assignment</p><p>*= Multiplication assignment/= Division assignment</p><p>%= Modulus assignment Decrement</p><p>The following program, ArithmeticDemo, tests the arithmetic operators.</p><p>class ArithmeticDemo {</p><p>public static void main (String[] args){</p></li><li><p>8/6/2019 Java in Easy Ways</p><p> 11/73</p><p>JJAAVVAA IINN EEAASSYY WWAAYYSS</p><p>Compilation : Randjithkumar, B.TechPage 11</p><p>int result = 1 + 2; // result is now 3System.out.println(result);</p><p>result = result - 1; // result is now 2</p><p>System.out.println(result);</p><p>resu...</p></li></ul>