0% found this document useful (0 votes)
44 views

CSE 201 Java Lecture 2 TC

This document provides an overview of key concepts related to packages, classes, and object-oriented programming in Java: - Classes can be organized into packages, and a package statement is used to specify the package for a class. Classes without a package statement are in the default unnamed package. - Classes in the same package can access each other without import statements, but classes from different packages require import statements. - Examples demonstrate creating a class in the "my_package" package and accessing it from another class using an import statement. - Primitive data types like int and boolean are covered, as are non-primitive types like classes and arrays. The differences between primitive and non-primitive types are explained.

Uploaded by

Palash Debnath
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

CSE 201 Java Lecture 2 TC

This document provides an overview of key concepts related to packages, classes, and object-oriented programming in Java: - Classes can be organized into packages, and a package statement is used to specify the package for a class. Classes without a package statement are in the default unnamed package. - Classes in the same package can access each other without import statements, but classes from different packages require import statements. - Examples demonstrate creating a class in the "my_package" package and accessing it from another class using an import statement. - Primitive data types like int and boolean are covered, as are non-primitive types like classes and arrays. The differences between primitive and non-primitive types are explained.

Uploaded by

Palash Debnath
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

CSE 201 Object Oriented Programming Language

Java: Basics

Notes on Packages
Java classes are arranged in a hierarchy or package structure ` We can place our classes into packages, but it is optional ` When we do not specify any package for our class, the class is placed in a default unnamed package ` To place a class in a package we use the package statement ` A class without any access modifier has package scope only ` A public class from a package can be used across packages (i.e., can be used from a class of another package => the key is import declaration)
`

Notes on Packages (Contd.)


Classes in the same package are implicitly imported into the source code files of other classes in the same package
`

Thus, an import declaration is not required when one class in a package uses another in the same package
`

An Example
/* Filename: A.java */ package my_package; public class A{ private int a; public A(int a){ this.a = a; } public void setA(int a){ this.a = a; } public int getA(){ return this.a; } } /* Filename: JavaTest.java */ import my_package.A; public class JavaTest{ public static void main(String[] args){ A a = new A(10); System.out.println(a.getA()); } }

Examining the Example


The declaration package my_package; in A.java puts the classes in this file into the package named my_package ` Class A is made public so it is accessible from outside the package my_package ` No package declaration at the top of JavaTest.java => class JavaTest is put under default unnamed package ` The import declaration import my_package.A; in JavaTest.java indicates that class A from my_package will be used in this file => if this declaration is missing then Compilation error at A a = new A(10); ` Then why no import for System class ??
`

Why no import is required for System class ` Because System resides in the package java.lang, which is automatically imported to every Java application. So, we can use all classes from java.lang in our program without explicitly using the import statement.
`

More on import The use of import is similar to the use of using namespace in C++.
`

To import all the classes from a package (e.g. java.util) we have to use import java.util.*;
`

Then what will we write to import all classes from my_package??


`

Taking input: A Sample Program (InputTest.java)


import java.util.Scanner; public class InputTest { public static void main(String args[ ]) { Scanner input = new Scanner(System.in); System.out.print(Enter an integer: ); int num1 = input.nextInt(); System.out.printf(You entered: %d\n, num1); } // end method main } // end class InputTest
// No need to use delete or free type of things. // Java manages memory allocation/deallocation automatically.

Examining InputTest.java
in of System.in is a static member of class System. It represents the standard input (similar to stdin or cin) ` Scanner is a class inside the Java API and is located in the java.util package ` Scanner contains some useful methods to read different types of data from the standard input (e.g. the keyboard) ` We have to use the import statement to tell the compiler that we will use Scanner from java.util in our program. Otherwise javac will not recognize Scanner
`

Examining InputTest.java (Contd.)


We use a Scanner object to wrap the standard input object System.in
`

We call the method nextInt( ) on the Scanner object input to read an integer from the keyboard
`

If we enter a non-integer value, an exception is thrown and the program terminates abnormally. Exception Handling is a major topic in Java and will be discussed elaborately later in the course
`

Primitive (built-in) Datatypes

Primitive (built-in) Datatypes


`

Integers
byte ` short ` int ` long
`

8 bit integer 16 bit integer 32 bit integer 64 bit integer 32 bit floating point number 64 bit floating point number

Real Numbers
float ` double
`

Others
char 16 bit, Unicode 2.1 character ` boolean true or false. false is not zero (0) in Java
`

Non-Primitive Datatypes
`

Non-primitive datatypes
Class Variables ` Array
`

Non-primitive types are also called Reference types ` Object Example class Cuboid{
`

private int l, w, h; public Cuboid(int a, int b, int c){l=a;w=b;h=c;} public static void main(String args[ ]){ Cuboid ob; // ob is a reference pointing to null ob = new Cuboid(10,20,5); // now the actual object is created }

Primitive vs. Non-Primitive Types


Primitive Types
`

Reference Types
`

Can store exactly one value of its declared type at a time. When another value is assigned to that variable initial value is replaced Primitive type instance variables are initialized by default
`

Instance variables of type byte, short, int, long, float, double char initialized to 0
Instance variables of type boolean initialized to false Note: Local variables are not initialized by default

Stores locations of objects in memory and are said to refer to objects. Objects that are referenced may contain many instance variables and methods Reference type instance variables are by default initialized to null
initialized by default

and ` Note: Local variables are not

` `

Primitive vs. Non-Primitive Types (Contd.)


Primitive Types
`

Reference Types
`

Primitive type variables are handled by value the actual values are stored in variables and passed to methods

All objects and arrays are handled by reference the references are stored in variables and passed to methods

Java References
Java references are used to point to Java objects created by new ` Java objects are always passed by reference to other functions ` In Java you can never pass an object to another function by-value ` Java references act as pointers but does not allow pointer arithmetic ` We cannot read the value of a reference and hence cannot find the address of a Java object ` We cannot take the address of a Java reference
`

Java References (Contd.)


`

But, we can make a Java reference point to a new object ` by copying one reference to another
ClassName ref1 = new ClassName(); ClassName ref2 = ref1;
`

by creating a new object using new and placing the returned address to the reference in question.
ClassName ref1 = new ClassName(); // first object ref1 = new ClassName(); // another object, address of first object is lost !!!

We cannot place arbitrary values to a reference except the special value null which means that the reference is pointing to nothing.
ClassName ref1 = 100; // compiler error ClassName ref2 = null; // no problem

Trying to use a null reference causes unexpected results or Exceptions in many cases ClassName ref2 = null; ref2.methodName();
`

Exception in thread "main" java.lang.NullPointerException

Type boolean
Java boolean type variables cannot be cast to another datatype
`

In Java, 0 and null are not the same as false and non-zero and non-null are not the same as true
`
`

Both true and false are keywords in Java

Does not have any relation with int


` ` `

boolean b = false; int a = b; // compiler error int a = (int)b; // compiler error

Operators (Precedence & Associativity)

Operators (Precedence & Associativity) (Contd.)

Operators (Precedence & Associativity) (Contd.)

Using Dialog Boxes


import javax.swing.JOptionPane; public class NameDialog { public static void main(String args[ ]) { String name = JOptionPane.showInputDialog(What is your name?); String message = String.format(Welcome, %s, name); JOptionPane.showMessageDialog(null, message); } }

Executing NameDialog

SumOfInt.java
/* FileName: SumOfInt.java */ import javax.swing.*; class SumOfInt { public static void main(String args[ ]) { String s1=JOptionPane.showInputDialog(null,"Enter 1st number:"); String s2=JOptionPane.showInputDialog(null,"Enter 2nd Number:"); int num1=Integer.parseInt(s1); int num2=Integer.parseInt(s2); JOptionPane.showMessageDialog(null,"Sum is : " + (num1+num2)); } }

Executing SumOfInt

Creating Your First Window (FirstWindow.java)


import java.awt.*;
import javax.swing.*; public class FirstWindow { public static void main(String args[]){ JFrame frame = new JFrame("My First Window"); JLabel label1 = new JLabel("I like Java", SwingConstants.CENTER); label1.setFont(new Font("ARIAL", Font.PLAIN, 72)); Font fontForButtons = new Font("ARIAL", Font.ITALIC, 24); JButton topButton = new JButton("Click me"); topButton.setFont(fontForButtons); JButton bottomButton = new JButton("Click me too"); bottomButton.setFont(fontForButtons);

FirstWindow.Java (Contd.)
Container c = frame.getContentPane(); c.add(label1); c.add(topButton, BorderLayout.NORTH); c.add(bottomButton, BorderLayout.SOUTH); frame.setSize(800, 600); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.setVisible(true);
} }

Output of FirstWindow.java

Lecture Content
`

Java: How to Program


`

Chapter 2, 3, 4

You might also like