Core Java Part III Computer Science YouTube Lecture Handouts

Doorsteptutor material for competitive exams is prepared by world's top subject experts: get questions, notes, tests, video lectures and more- for all subjects of your exam.

Topics to be covered

Taking Input from Console- Scanner Class

We use scanner class to accept values from the console during run-time. We can use this class by making object of the scanner class.

For Example:

Scanner s = new Scanner (System.in) ;

Scanner class is a predefined class which is defined in java. util package. That՚s why to use Scanner class, we have to import java. util package.

Code

Import java. util. ⚹;

Here java is main package and util is a sub package created inside. Package, sub package and class is always separated with dot (.) .

Table Supporting: Taking Input from Console- Scanner Class
Function⟋ Method NameUse of the Method
nextlnt ()It is used to scan the integer value as an input from the console.
nextFloat ()It is to scan the float (in decimal from) value as an input from the console.
nextDouble ()It is used to scan the double (in decimal from) value as an input from the console.
nextLong ()It is used to scan the long (integer) value as an input from the console.
nextShort ()It is used to scan the short (integer) value as an input from the console.
nextBoolean ()It is used to scan the boolean value as an input from the console.
nextLine ()It is used to scan the string value as an input from the console.

Pre-Defined Methods of Scanner Class

Illustration: Pre-Defined Methods of Scanner Class

Code⟋Output

Illustration: Pre-Defined Methods of Scanner Class
Illustration: Pre-Defined Methods of Scanner Class

Oriented Approach Object

As we know, Java is an object oriented language. It supports various object oriented concepts that are as follows:

  • Object and Classes
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Classes & Objects

  • In java programming, we can use multiple classes in a single program. Classes can be used by creating their objects and using them to call its member function through main class.
  • As discussed earlier there must be a main class having main function.
Illustration: Classes & Objects
Illustration: Classes & Objects

Functions ⟋Methods

Functions are methods that are created for some specific tasks. Function is a block⟋container that holds some statements and invoked when it is called.

It can be a public, private, protected, default, final and static. Its specifier defines whether it is accessible from outside the class or not. It can be both pre-defined and user-defined

Illustration: Functions ⟋Methods

Variable – Local Variable

Local variable is defined inside a function or a method. It is not accessible or used by the another function of the class. It is defined and initialized within the class scope and also destroyed when the function is completed.

Illustration: Variable – Local Variable

Variable- Instance Variable

Instance variable is defined and can be accessed within the class scope. It can be used by any constructor or method of the same class but cannot be used outside the class.

Illustration: Variable- Instance Variable

Variable-Class Variable

Class variable is declared within a class and can be accessed within a class by any method, constructor, also outside the class. It can be declared using static keyword. That՚s why this is also called static variable. They always initialized whenever a class is instantiate and destroyed when class execution is completed.

Illustration: Variable-Class Variable

MCQs

Q-1. Which type of variable can be accessed within the class by any member function or outside the class scope?

1. Private

2. Static

3. Default

4. Final

Answer: 2

Q-2. Which is not a function of Scanner class?

1. nextInteger ()

2. nextFloat ()

3. nextDouble ()

4. nextShort ()

Answer: 1

Mayank