Oop lecture2

8
Java compilation process Lecture 2 Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria

Transcript of Oop lecture2

Page 1: Oop lecture2

Java compilation process

Lecture 2

Object Oriented Programming

Eastern University, DhakaMd. Raihan Kibria

Page 2: Oop lecture2

Source-to-machine code

Steps: Java source—ClassDemo.java Compiler (javac) – output is bytecode

ClassDemo.class Run (java) – ClassDemo.class is interpreted

and converted into executable

Page 3: Oop lecture2

Java Virtual Machine

It is a piece of software also called JVM Understands byte code produced by javac Interprets the byte code and runs program Cross-platform (windows, linux, mac)

compatibility

Page 4: Oop lecture2

Difference between JDK and JRE

JDK is Java Development Kit Needed to compile (javac)

JRE is Java Run-time environment Needed to run programs/byte-code

Page 5: Oop lecture2

Difference between object code and byte code

Object code: Typically has an extension *.exe (in windows) Produced by compiler During run-time no more interpretation is needed

because output is already in 1's and 0's Machine/OS dependent

Byte code: Has an extension *.class regardless of operating

system Produced by java compiler The byte-code is still not in 1's and 0's. Rather JVM

needs to convert the .class files into 1's and 0's Byte code is machine/OS independent

Page 6: Oop lecture2

Advantage of byte code representation

Machine/OS independent Can be compiled “Just In Time” (JIT) Can run on browsers as applets Can be made faster by JIT compiler

optimization techniques

Page 7: Oop lecture2

Example of a java desktop application

Also called java swing applicationimport java.awt.FlowLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextField;

public class SwingDemo { public static void main(String[] args) { JFrame jframe = new JFrame(); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setBounds(0, 0, 300, 200); jframe.getContentPane().setLayout(new FlowLayout());

JButton jbutton = new JButton("Test button"); jframe.getContentPane().add(jbutton);

JTextField jtext = new JTextField(); jframe.getContentPane().add(jtext); jtext.setText("Hello"); jframe.setVisible(true); }}

Page 8: Oop lecture2

Output