--Your friends at LectureNotes

Note for Java Programming - JAVA By Amity Kumar

  • Java Programming - JAVA
  • Note
  • Amity University - AMITY
  • Computer Science Engineering
  • 11 Topics
  • 393 Offline Downloads
  • Uploaded 2 years ago
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-3

UNIT-1 INTRODUCTION TO JAVA 1. Introduction to Java • Java is an object-oriented programming language developed by Sun Microsystems, a company best known for its high-end Unix workstations. • Java is modeled after C++ • Java language was designed to be small, simple, and portable across platforms and operating systems, both at the source and at the binary level (more about this later). • Java also provides for portable programming with applets. Applets appear in a Web page much in the same way as images do, but unlike images, applets are dynamic and interactive. • Applets can be used to create animations, figures, or areas that can respond to input from the reader, games, or other interactive effects on the same Web pages among the text and graphics. 1.1 Java Is Platform-Independent Platform-independence is a program's capability of moving easily from one computer system to another. • Platform independence is one of the most significant advantages that Java has over other programming languages, particularly for systems that need to work on many different platforms. • Java is platform-independent at both the source and the binary level. 1.2 Java Development Kit (JDK)- Byte code • Bytecodes are a set of instructions that look a lot like machine code, but are not specific to any one processor • Platform-independence doesn't stop at the source level, however. Java binary files are also platform-independent and can run on multiple platforms without the need to recompile the source. Java binary files are actually in a form called bytecodes. 1.3 Object-Oriented Programming • Many of Java's object-oriented concepts are inherited from C++, the language on which it is based, but it borrows many concepts from other object-oriented languages as well. Page 1

Text from page-4

• Java includes a set of class libraries that provide basic data types, system input and output capabilities, and other utility functions. • These basic classes are part of the Java development kit, which also has classes to support networking, common Internet protocols, and user interface toolkit functions. • Because these class libraries are written in Java, they are portable across platforms as all Java applications are. 1.4 Creating a simple Java Program Hello World example : class HelloWorld { public static void main (String args[]) { System.out.println("Hello World! "); } } This program has two main parts: • All the program is enclosed in a class definition—here, a class called Hello World. • The body of the program (here, just the one line) is contained in a method (function) called main(). In Java applications, as in a C or C++ program, main() is the first method (function) that is run when the program is executed. 1.5 Compiling the above program : • In Sun's JDK, the Java compiler is called javac. javac HelloWorld.j ava • When the program compiles without errors, a file called HelloWorld.class is created, in the same directory as the source file. This is the Java bytecode file. • Then run that bytecode file using the Java interpreter. In the JDK, the Java interpreter is called simply java. java HelloWorld If the program was typed and compiled correctly, the output will be : "Hello World!" Page 2

Text from page-5

2. Variables and Data Types • Variables are locations in memory in which values can be stored. They have a name, a type, and a value. • Java has three kinds of variables: instance variables, class variables, and local variables. • Instance variables, are used to define attributes or the state for a particular object. Class variables are similar to instance variables, except their values apply to all that class's instances (and to the class itself) rather than having different values for each object. • Local variables are declared and used inside method definitions, for example, for index counters in loops, as temporary variables, or to hold values that you need only inside the method definition itself Variable declarations consist of a type and a variable name: Examples : in t myA ge; St ri n g my N a me; boolean isTired; 2.1 Integer types. T y pe Si z e R a ng e byte 8 bits —128 to 127 short 16 bits —32,768 to 32,767 int 32 bits —2,147,483,648 to 2,147,483,647 —9223372036854775808 to 9223372036854775807 long 64 bits 2.2 Floating-point This is used for numbers with a decimal part. Java floating-point numbers are compliant with IEEE 754 (an international standard for defining floating-point numbers and arithmetic). Page 3

Text from page-6

There are two floating-point types: float (32 bits, single-precision) and double (64 bits, double-precision). 2.3 Char The char type is used for individual characters. Because Java uses the Unicode character set, the char type has 16 bits of precision, unsigned. 2.4 Boolean The boolean type can have one of two values, true or false. Note that unlike in other C-like languages, boolean is not a number, nor can it be treated as one. All tests of Boolean variables should test for true or false. 2.5 Literals Literals are used to indicate simple values in your Java programs. Number Literals • There are several integer literals. 4, for example, is a decimal integer literal of type int • A decimal integer literal larger than an int is automatically of type long. • Floating-point literals usually have two parts: the integer part and the decimal part—for example, 5.677777. Boolean Literals Boolean literals consist of the keywords true and false. These keywords can be used anywhere needed a test or as the only possible values for boolean variables. 2.6 Character Literals Character literals are expressed by a single character surrounded by single quotes: 'a', '#', '3', and so on. Characters are stored as 16-bit Unicode characters. 3. Expressions and Operators • Expressions are the simplest form of statement in Java that actually accomplishes something. Expressions are statements that return a value. • Operators are special symbols that are commonly used in expressions. Page 4

Lecture Notes