A Short Introduction to JAVA Kazi Masudul Alam
JAVA Object-oriented programming language developed by Sun Microsystems. Currently owned by ORACLE (ORACLE bought Sun Microsystems) Designed to be small, simple, and portable across platforms and operating systems Source, binary work in Windows, Linux, Mac OS Android OS and apps are motivated from JAVA like language. In 1991, James Gosling and Sun Microsystems began designing a language for home appliances (toasters, TVs, etc.). challenging, because home appliances are controlled by many different chips (processors) Programs were translated first into an intermediate language common to all appliance processors. Then the intermediate language was translated into the machine language for a particular appliance’s processor. Appliance manufacturers weren’t impressed. In 1994, Gosling realized that his language would be ideal for a Web browser that could run programs over the Internet. Sun produced the browser known today as HotJava.
Different Editions JAVA Standard Edition (J2SE) Enterprise Edition (J2EE) Micro Edition (J2ME) J2SE is for desktop applications. We will be using this version of JAVA J2ME is for embedded systems ranging from Mobile Phone, Watch, Set top box, Microwave, Fridge etc. J2EE is for enterprise applications with heavily distributed nature. Very important to know the stacks and techniques used. Highly demanded in the job market.
JAVA Architecture
JAVA Application Development A compiler translates a program from a high-level language to a low-level language the computer can run. The Java compiler does not translate a Java program into assembly language or machine language for a particular computer. Instead, it translates a Java program into byte-code. Byte-code is the machine language for a hypothetical computer (or interpreter) called the Java Virtual Machine. A byte-code program is easy to translate into machine language for any particular computer. A program called an interpreter translates each byte-code instruction, executing the resulting machine-language instructions on the particular computer before translating the next byte-code instruction. After compiling a Java program into byte-code, that byte-code can be used on any computer with a byte-code interpreter and without a need to recompile. Byte-code can be sent over the Internet and used anywhere in the world.
JAVA Editors DrJava Nebeans IDE Eclipse Others www.drjava.org (A light weight IDE) Nebeans IDE https://netbeans.org/downloads Eclipse https://eclipse.org/downloads/ Others Blue, JCreator, EJE Eclipse is the most used editor. It can work with many other programming language. Also, many other research works develop their plugins to design the systems using Eclipse
DrJAVA
J2SE Development Java SE Development Kit (8/7/6) required to develop and compile the code Java SE Runtime Environment (8/7/6) required to execute the program Download here http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
First Java program public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!"); }
Saving and Running Save it as HelloWorld.java. Again, make sure that the filename is the same case as the class name. Compile and run it: javac HelloWorld.java Compiling will generate HelloWorld.class file java HelloWorld java HelloWorld.class Your computer should display Hello, world!
Errors An error in a program is called a bug. Eliminating errors is called debugging. three kinds or errors syntax errors runtime errors logic errors Syntax error grammatical mistakes in a program the grammatical rules for writing a program are very strict The compiler catches syntax errors and prints an error message. example: using a period where a program expects a comma Runtime error errors that are detected when your program is running, but not during compilation When the computer detects an error, it terminates the program an prints an error message. example: attempting to divide by 0 Logic error errors that are not detected during compilation or while running, but which cause the program to produce incorrect results
Sum of 2 numbers class FirstProgram
Samples – Data Type
Samples – Control Statement
Samples – Loop
Variable Types Local variables Instance variables Static variables Constant
Modifier Types Access Modifier Visible to the package, the default. No modifiers are needed. Visible to the class only (private). Visible to the world (public). Visible to the package and all subclasses (protected).
Modifier Types Non-Access Modifier The static modifier for creating class methods and variables The final modifier for finalizing the implementations of classes, methods, and variables. The abstract modifier for creating abstract classes and methods. The synchronized and volatile modifiers, which are used for threads
Arithmetic Operators Output:
Relational Operators
Logical Operators
JAVA Numbers xxxValue() Converts the value of this Number object to the xxx data type and returned it valueOf() Returns an Integer object holding the value of the specified primitive. toString() Returns a String object representing the value of specified int or Integer. parseInt() This method is used to get the primitive data type of a certain String sqrt() Returns the square root of the argument. random() Returns a random number.
JAVA Character and String isLetter() Determines whether the specified char value is a letter isDigit() Determines whether the specified char value is a digit. isUpperCase() Determines whether the specified char value is uppercase isLowerCase() Determines whether the specified char value is lowercase
JAVA Character and String compareTo(String anotherString) Compares two strings lexicographically. endsWith(String suffix) Tests if this string ends with the specified suffix byte[] getBytes(String charsetName) Encodes this String into a sequence of bytes using the named charset int indexOf(int ch) Returns the index within this string of the first occurrence of the specified character. int length() Returns the length of this string. boolean matches(String regex) Tells whether or not this string matches the given regular expression.
JAVA Array dataType[] arrayRefVar =new dataType[arraySize]; double[] myList =new double[10];
JAVA Methods Passing parameter by value
JAVA Files and I/O
Input / Output
Exception Handling
JAVA Multi Threading
Other Features Package Interface Encapsulation Abstraction Polymorphism Over riding Over loading Inheritance
LAB Fill the required logic in the given code so that the output of the program matches. Only use control statements for/while/do-while if/else if/else break/continue Dot not use 2D Array! Write the out patterns in a file. Then read the file and print it in screen.
Lab Outputs Desired Output Sample Code
References Thanking. Prof. Ivan Stojmenovic http://www.learnjavaonline.org/ http://www.tutorialspoint.com/java/java_tutorial.pdf