CORE JAVA PART II Computer Science YouTube Lecture Handouts

Dr. Manishika Jain- Join online Paper 1 intensive course. Includes tests and expected questions.

Topics to be discussed

  • Identifiers, Variables and Constants
  • Data Types
    • Primitive
    • Non-Primitive
  • Printing Statement
  • Comments in Java
  • Wrapper class
  • JAVA Packages
  • Access Specifiers

Identifiers

An identifier is a name used for a variable, an interface, a function or a class. There are some set of rules in JAVA to use an identifier.

  • An identifier must start with an alphabet (uppercase/lowercase) or an underscore or ‘$’ dollar sign. After the first character, it can have numbers also.
  • It cannot contain any white space.
  • Any reserved word cannot be used and an identifier.
  • Identifiers are case sensitive that means ‘num1’ and ‘NUM1’ are different.
  • An identifier should be meaningful means always related to the value they are holding. Else they will be unhandled in complex programming.

Examples: $ Income, Emp_Income, dob_as_1999 are some valid identifiers

Variables vs Constants

  • VARIABLE: A variable is a memory location which stores data temporarily. Temporarily means value hold by a variable can be change any time before or during the program execution
  • CONSTANT: Constant is a variable whose value cannot be changed before or during the execution of the program. If you want to make a variable ‘constant’ , add a specifier ‘final’ with the variable. Value of final variable can՚t be changed.

Code/Output – Constant Variable

CODE/Output – Constant Variable
CODE/Output – Constant Variable

Code/Output- a Variable

CODE/OUTPUT- a VARIABLE

Data Types

  • Data types are used to specify the types of data holding by a variable. In JAVA, there are two types of data types.
    • Primitive Data Types
    • Non-Primitive Data Types

Primitive Data Type

  • Primitive data types are pre-defined in Java. That Means Pre-defined data type can hold data of particular length/size.
  • There are eight types of Primitive data types in JAVA.
Primitive Data Type

Non-Primitive Data Types

Non-Primitive data types are not pre-defined. They are user-defined. It means user can store data as per his requirements.

Non-Primitive data types are of two types

  • String- It can contain data in the form of ‘characters, words, sentences’
  • Array- It can contain multiple value of one type

Printing Statement

  • To print a message, we use pre-defined statement i.e.. System. out. print ( “Welcome to JAVA World” ) ;
  • To print a message in new line, we use System. out. println ( “Welcome to JAVA World” ) ;
  • To print a message with a value of a variable, we use concatenate symbol i.e.. ‘+’ .
Printing Statement

Comments in Java

Comments are non-executable code that is written for better understanding the code. These are just created for reference. There are two types of comments in JAVA.

Multiline Comments- To create multiline non-executable code:

/ ⚹ …

. .

Statement (s) … ⚹ /

Single line Comment- To create single line non-executable code:

// statement

Wrapper Class

Wrapper classes are the classes whose objects contains primitive data types.

Wrapper Class

Java Packages

A Package is collection of classes, interfaces, sub-packages.

  • It favours a mechanism called encapsulation that means data hiding. Because classes, interfaces and sub-packages are hidden inside the package. To use a particular class inside a package, you have to import that package. For example , to access any class form the sub-package ‘util’ which is created in the package ‘java’ , we use:
  • Import java. util. ⚹ ;
  • Package can be of two type:
    • User-defined package
    • Pre-defined package
Java Packages

Access Specifiers

Access Specifiers controls the accessibility of class and its data members. There are four types of access specifies in JAVA.

END of the PRESENTATION
Sr. No.Access SpecifierAccessibility
1publicPublic data can be accessed anywhere within the package/class, outside the package/class.
2privateIt cannot be accessed from outside from the class.
3protectedIt can be accessed within the package, outside the package through child class only. Protected data can be accessed within the class or by derived class else it is restricted to access protected data from outside the class.
4defaultIt is accessible within the package, but not from outside the package. If any specifier name is not mentioned, then will be default.

MCQs

Q-1. Which Access Specifier allows only derived class to access the data members?

1. Public

2. Private

3. Default

4. Protected

Answer: 4

Q-2. Which data type can hold the value as true or false only?

1. int

2. boolean

3. double

4. float

Answer: 2

Developed by: