Docsity

Introduction to Java Programming: Syntax, Methods, and Basic Concepts, Lecture notes of Computer Science

Partial preview of the text

Download Introduction to Java Programming: Syntax, Methods, and Basic Concepts and more Lecture notes Computer Science in PDF only on Docsity!

Object Oriented

Programming

2(JAVA)

BASIC CONCEPTS OF JAVA

Java - General

 Java is:

 platform independent programming language

 similar to C++ in syntax

 similar to Smalltalk in mental paradigm

 Pros: also ubiquitous to net

 Cons: interpreted, and still under

development (moving target)

Object-Oriented

 Java supports OOD

 Polymorphism

 Inheritance

 Encapsulation

 Java programs contain nothing but

definitions and instantiations of classes

 Everything is encapsulated in a class!

Java Advantages

 Portable - Write Once, Run Anywhere  Security has been well thought through  Robust memory management  Designed for network programming  Multi-threaded (multiple simultaneous tasks)  Dynamic & extensible (loads of libraries)

 Classes stored in separate files
 Loaded only when needed

Primitive Types and Variables

 boolean, char, byte, short, int, long, float, double

etc.

 These basic (or primitive) types are the only types

that are not objects (due to performance issues).

 This means that you don’t use the new operator to

create a primitive variable.

 Declaring primitive variables:

float initVal; int retVal, index = 2; double gamma = 1.2, brightness boolean valueOk = false;

Initialisation  If no value is assigned prior to use, then the compiler will give an error  Java sets primitive variables to zero or false in the case of a boolean variable  All object references are initially set to null  An array of anything is an object  Set to null on declaration  Elements to zero false or null on creation

Basic Mathematical Operators

 * / % + - are the mathematical operators  * / % have a higher precedence than + or -

double myVal = a + b % d – c * d /

b;

 Is the same as:

double myVal = (a + (b % d)) –

((c * d) / b);

Statements & Blocks

 A simple statement is a command terminated by a

semi-colon:

name = “Fred”;

 A block is a compound statement enclosed in curly

brackets:

name1 = “Fred”; name2 = “Bill”;

 Blocks may contain other blocks

EXAMPLE

 public class Main {

 public static void main(String[] args) {

 System.out.println("WELCOME TO KYU UNIVERSITY");

Note: We can use the println() method

to print a line of text to the screen:

In summary

Java User Input

The Scanner class is used to get user input, and it is found in

the java.util package.

To use the Scanner class, create an object of the class and use any

of the available methods found in the Scanner class

documentation. In our example, we will use the nextLine() method,

which is used to read Strings:

Example

import java.util.Scanner; // Import the Scanner class class Main { public static void main(String[] args) { Scanner myObj = new Scanner(System.in); // Create a Scanner object System.out.println("Enter username"); String userName = myObj.nextLine(); // Read user input System.out.println("Username is: " + userName); // Output user input } }

Example

import java.util.Scanner; class Main { public static void main(String[] args) { Scanner myObj = new Scanner(System.in); System.out.println("Enter name, age and salary:"); // String input String name = myObj.nextLine(); // Numerical input int age = myObj.nextInt(); double salary = myObj.nextDouble(); // Output input by user System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Salary: " + salary); }

Add two Numbers

1. public class SumOfNumbers 1

  1. { 3. public static void main(String args[])
  2. { 5. int n 1 = 225 , n 2 = 115 , sum;
  3. sum = n 1 + n 2 ;
  4. System.out.println("The sum of numbers is: "+sum);
  5. }
  6. }