Java Basics Java Basics: Dr. Anjali Assistant Professor Dr. Anjali Assistant Professor

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

OOPS

Lecture 2:
Java Basics

Dr. Anjali
Assistant Professor
ACCESSING LIBRARIES AND TAKING
INPUT
Accessing Pre-Created Java
Libraries

• It’s accomplished by placing an ‘import’ of the appropriate library at the top of


your program.
• Syntax:
import <Full library name>;

• Example:
import java.util.*;
Getting Text Input
• You can use the pre-written methods (functions) in the Scanner class.
• General structure:
import java.util.Scanner;
Creating a
scanner object
main (String [] args) (something that
can scan user
{ input)
Scanner <name of scanner> = new Scanner (System.in);
<variable> = <name of scanner> .<method> ();
}

Using the capability of the


scanner object (actually
getting user input)
Getting Text Input (2)
The name of the online example: Main.java
import java.util.*;
public class Main{
public static void main (String [] args) {
String str1;
int num1;
Scanner in = new Scanner (System.in);
System.out.print ("Type in a line: ");
str1 = in.nextLine ();
System.out.print ("Type in an integer: ");
num1 = in.nextInt ();
System.out.println ("num1:" +num1 +"\t str1:" + str1);
}
}
Useful Methods Of Class
Scanner1

nextBoolean() Reads a boolean value from the user

nextByte() Reads a byte value from the user

nextDouble() Reads a double value from the user

nextFloat() Reads a float value from the user

nextInt() Reads a int value from the user

nextLine() Reads a String value from the user

nextLong() Reads a long value from the user

nextShort() Reads a short value from the user


Reading A Single Character

• Text menu driven programs may require this capability.


• Example:
GAME OPTIONS
(a)dd a new player
(l)oad a saved game
(s)ave game
(q)uit game

• There’s different ways of handling this problem but one


approach is to extract the first character from the string.
• Partial example:
String s = "boo“;
System.out.println(s.charAt(0));
Reading A Single Character
• Name of the (more complete example): MyInputChar.java
import java.util.*;
public class Main
{
public static void main (String [] args)
{
final int FIRST = 0;
String selection;
Scanner in2 = new Scanner (System.in);
System.out.println("GAME OPTIONS");
System.out.println("(a)dd a new player");
System.out.println("(l)oad a saved game");
System.out.println("(s)ave game");
System.out.println("(q)uit game");
System.out.print("Enter your selection: ");
selection = in2.nextLine ();
System.out.println ("Selection: " + selection.charAt(FIRST));
}
}
Variables
A variable is a location in memory (storage area) to hold data.

Note: Java is a statically-typed language. It means that all variables must be declared before
they can be used.
Rules for Naming Variables in
Java

• Java is case sensitive. Hence, age and AGE are two


different variables.
• Variables must start with either a letter or an
underscore, _ or a dollar, $ sign.
• Variable names cannot start with numbers.
• Variable names can't use whitespace.
Pop Quiz?

• int good-bye;
• int shrift = 0;
• char thisMustBeTooLong;
• int bubble = 0,toil = 9, trouble = 8
• int 8ball; //
• int double; //
Answers

• int good-bye; //bad variable name


• int shrift = 0; //OK
• char thisMustBeTooLong= ‘k’; //OK in syntax //but poor
choice in variable name
• int bubble = 0,toil = 9, trouble = 8
// “;” missing at the end
• int 8ball; //can’t start with a digit
• int double; //double is a reserve word
Variables (Contd.)

• There are 4 types of variables in Java programming


language:
• Instance Variables (Non-Static Fields)
• Class Variables (Static Fields)
• Local Variables
• Parameters
Literals
Literals are data used for representing fixed values. They can be used directly in the
code. For example,

int a = 1;
float b = 2.5;
char c = 'F';

Here, 1, 2.5, and 'F' are literals.


Literal Types
Boolean
Literals

Integer
Literals

Literals
Floating-
String
point
literals
Literals
Character
Literals
Primitive Data Types
Constants
Program

// this program will declare and print a number


public class int2
{
public static void main(String[] arguments)
{
int weight = 68;
final int dob_dd=13;
weight= 68+20;
dob_dd=dob_dd-1;
System.out.println("your weight is " + weight);
}
}
//end of program
18
Type checking
Type conversion with built-in
types
Pop quiz on type conversion
Pop quiz on type conversion
Summary

You might also like