Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Java Programming

The document provides a syllabus for Java programming that covers programming fundamentals like variables, data types, operators, conditions, loops, and methods. It then discusses object-oriented programming concepts like classes, objects, abstraction, encapsulation, inheritance, and polymorphism. The document also explains why Java is a useful language to learn and provides an overview of Java programming.

Uploaded by

Aman Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Java Programming

The document provides a syllabus for Java programming that covers programming fundamentals like variables, data types, operators, conditions, loops, and methods. It then discusses object-oriented programming concepts like classes, objects, abstraction, encapsulation, inheritance, and polymorphism. The document also explains why Java is a useful language to learn and provides an overview of Java programming.

Uploaded by

Aman Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

JAVA PROGRAMMING

SYLLABUS
Programming Fundamentals

 Variables
 Data types
 Operators
 Conditions
 Loops
 Methods

Object-Oriented Programming

 Concrete Classes
 Objects
 Abstraction
 Encapsulation
 Inheritance
 Polymorphism
 Exceptions
 Abstraction Classes
 Interfaces
 File I/O
Why Learn JAVA

 High-level,General-Purpose,O-O programming Language


->Easy&used to develop any kind of programs.
 Very Popular.
->A huge online community for getting help.
 Can be used in Android development.
 C based language
->learn C/C++/C# easier.

Program (Software)

Set of instruction that tell a computer what to do

 We use programs to interact/talk with computers.


 To write programs we use programming languages.

Programming Languages

Languages used to write programs

 Computer are machines.


->they do not understand human languages.
 Programs are written in a language a computer can
understand.
->programming language.

Machine Language

A computer’s native language

 Uses zeros & ones(0|1)-> Binary Language.


 To add 2 and 3 and get the result :1101101010011010.
->Very hard to use.
 Machines dependent | differs among different types
of machines.
 Every instruction should be written in machine language
before it can be executed
->All instruction written in other programming
languages must be translated to machines code
instruc..

Assembly Language

Was developed to make programming easier

 Machine dependent.
 Introduced keyword | add,sub,..
 To add 2 and 3 and get the result: add 2,3,result
 A program called ‘assembler’ translates assembly code
to machine code.

Assembler

High-Level Language

A new generation of programming languages

 Uses English words | Easy to learn & use.


 Machine independent | your program will run on
different machines.
 Instruction are called ‘statements’.
 A program written in a high-level language is called
‘source program’ or ‘source code’.
 To add 2 and 3 and get the result: result = 2+3;
 A ‘compiler’ or an ‘interpreter’ is used to translate
source code to machine code.

Compiler

Translates all the source code into machine code

SOURCE PROGRAM->EXECUTABLE

Source code-COMPILER->Machine code-EXECUTOR->output

Interpreter

Translates each statement into machine code and executes


it right away

Statement-INTERPRETER->OUTPUT

About Java

 Developed at Sun Microsystem which was purchased by


Oracle in 2010.
 General-purpose & powerful programming language.
 Used for developing software that runs on mobile,
desktop, and servers.
 Machine independent.

Java language specification

The syntax and semantics of java

 To write we should follow some rules (Grammar,…).


 Also, to write java we should follow some rules->syntax
& semantics.
 He are playing -> syntax error. (Grammar)
 He is hello and bye -> semantic error. (Meaning)
API

Application programming interface

 Also known as a ‘library’.


 Contains predefined java code that we can use to
develop java programs.
-> Faster and easier development process | no need to
write everything from scratch.

Editions of java

Java comes in three editions

 Java Standard Edition (SE): develop application that


run on desktop.
 Java Enterprise Edition (EE): develop server-side
application.
 Java Micro Edition (ME): develop application from
mobile devices.

JDK

Java development kit

 Set of programs that enable us to develop our


programs.
 Contains JRE (Java Runtime Environment) that is used
to run our programs.
 JRE & JDK contains JVM(Java Virtual Machine).
 JVM executes our java programs on different
machines.
->java is independent.
IDE

Integrated development environment

A program that allows us to:

 Write | source code.


 Compile | machine code.
 Debug | tools to find errors.
 Build | files that can be executed by JVM.
 Run | execute our program.
->Development is faster and easier.
Popular Java IDEs: Netbeans, Eclipse, IntelliJ IDEA,…

Anatomy of Java

CLASS – A blueprint to create OBJECTS

OBJECTS – An instance of a CLASS

Class Structure

‘class’ is a keyword(syntax).
Method

Group of instruction to do a specific task

Examples:

 A method to add two numbers.


 A method to say Hi to the user.
 A method to get the user’s name.
We have a special method called ‘main’.

Method Structure

Each method consists of 4 main parts

Note: every method is written inside a class.

Calling a method

Is basically using the method

->The code block of this method will be executed.

Note: the main() method is automatically called when we run


our java program.

-> it is the first method that is called.

-> it is the starting point of execution of our program


Access Modifiers

Specify how to access Classes & Methods

(Discussed later in OOP)

 Public
 Private
 Protect
 Default

Naming Conventions

How to write names in programming

Pascal case convention:

->ThisIsAName

Camel case convention:

->thisIsAName

Snake case convention:

->this_is_a_name

Java Program Structure


Remember

 Every java program contains at least one class.


 Pascal case is used with classes.
 Camel case is used with methods.
 A method exists inside a class.
 main() is the starting point of execution of our program

Package

A container for classes

Strings

Group of characters (Text)

Strings in java should be put in double quotes.

Examples:
Calling println()

Displays its parameter on the console window

‘ln’ is to break to a new line.

-> No line break

What is System.out.?

 out is an object of the ‘PrintStream’ Class.


 out has the print() and println() methods.
->use’.’ to access print()/println() of out.
 Out refers to the standard output device.(screen).
 System is a class (pascal case)
 Out is inside System (field).
->use’.’ to access out of System.
->System.out.println().

Numbers

Numbers in java are just like in mathematics

Examples: 0,-19,129,-0.5,90.81
Arithmetic operators

Addition operator: +

Subtraction operator: -

Division operator: /

Multiplication operator: *

Printing some values

The value will be calculated and then the result will be


printed

Creating, Compiling, & Executing a Java Program

 Write java code in notepad

Save the file as Main.java on your Desktop.


 Compile & run main java using CMD

 What happens exactly?

Comments

Text ignored by the compiler

 Write notes
 Find errors
Access Modifier

Public Access Modifier

Specify how to access classes, methods, and fields

The access level is everywhere

 Inside a class
 Outside a class
 Inside the package
 Outside the package

Private Access Modifier

The access level is only inside the class

Static Non-Access Modifier

You can access fields/methods using the class name.

Example: System.out

 out is a static field of System

Return Types

The type of data that a method return/gives

 A method can return/give a value.


 Consider this mathematical function: f(x) = x+1
-> x is a parameter to this function.
-> x+1 is the return value of this function.
-> This function return a number (return type).
 The same thing applies for method/function in
programming.
Examples:

 getUserName();
-> Get te name of the user and return the value.
-> The return type is a text (String).
 getUserAge();
-> Get the age of the user and return the value.
-> the return type is a number (int, double, …).

The void return type

Main() has a void return type.

-> void means nothing.

-> main() does not return a value.

Examples:

 printUserName();
-> We just want to print the name of the user.
-> we do not want to get any value from this method.
-> The return type is void.
 Note that every method has a very specific role.
-> getUserName();
-> printUserName();
Command line arguments

Data given to the main method


Programming errors
JAVA BASICS

1. Variables

A container that stores some data

 Our computer has a memory where we can store things.


 To be able to store something in the memory we have
to reserve some space. -> Variable.
 Each variable has a specific type.
 It is classed a variable because the value inside it can
change.

2.Data types

We have different types of data

Example:

 Text.
 Number.
 Boolean.(true/false)
 User defined types.(car,person,…)

These types are also for variables.

3.Operators

Arithmetic, relational, and logical operators

 Arithmetic: to do some calculations.(+,-,…)


 Relational: to do some comparisons.(<,>,=,…)
 Logical: to combine conditions together.(AND,OR,…)
4.Conditions

Used to control what to execute in our program

 If a condition is true -> do something.


 If it is not true -> do another thing.

5.Loops

Used to run some code more than once

 While loop.
 Do while loop.
 For loop.
 For each loop.

6.Methods

Divide our code into smaller pieces

 Less code in main().


 Our program will be easier to maintain and debug.
 Our program will be easier to understand.
 Makes the development process easier by breaking our
program to smaller pieces an solving them one by one.

VARIABLES

1.Memory-A variable is like a box in the computer’s memory


2.Declaration

Allocating space inside our memory

 To allocate space in our memory we declare a variable


-> TYPE NAME;
 The type of the variable should be compatible with
data inside it
->to store a string inside a variable, the variable’s type
should be string
 String myName;
->Declared a variable called myName and can store a
string
 To declare multiple variables of the same type:
 TYPE NAME1. NAME2;
 String myName, myJob;
->myName & myJob are two variables tha can store a
string.
 A variable must be declared before it can be used.

3.Assignment

Used to store/put a value inside a variable

 We can assign a value to a variable by using the


assignment operator(=).
 variableName = expression
-> myJob = “programmer”;
-> ”programmer” will be stored inside myJob.
 An expression is anything that produces/gives a value.
Example: (1+3), (4*2).
4.Initialization

Assigning a value to a variable when declaring it

Example: String myJob = “programmer”;

This is equivalent to:

String myJob;

myJob = “programmer”;

5.Remember (Be Careful)

Do not re-define variables

Consider the following code:

 Each variable has a unique name.


 When assigning a value to a variable do not define it
again.

Always initialize your variables!

 Less error.
 Less code.
 More readable code.
Constants

A variable whose value can not be changed

 To define a constant we use the final keyword


 Constant can be used like any other variable
 Constants names are written in upper case and using
the snake case convention
 You will get a syntax error if you try to change the
value of a constant.

Initializing constants

final TYPE NAME = VALUE;

Benefits of using constants

1. The value will not be changes by accident.


2. You don’t have to type the same value of it is used
multiple times.
3. A descriptive names for a constant makes the program
easy to read and understand.

Indentifiers

Identidiers are the names that identify the element in a


program

 Names of classes
 Names of method
 Names of variables
myName, myJob, sayName, Main, …
Naming rules

Every identifies must obey some rules

 Can contain letter, digit, underscores(_), and dollar


signs($)
-> myName, my_Name2, my$
 Must start with a letter, or an underscore, or a dollar
sign. It cannot start with a digit and it can not contain
spaces.
-> hello, i, _myName, $1, $myAge ->OK
-> 1hello, @hi, *name, my age -> NOT OK
 Cannot be reserved word
-> main, class, String, …

Example

Legal Illegal
1. $2 1. 2A
2. Person 2. a+b
3. Area 3. user name
4. Radius 4. int
5. Point 5. main
6. hello 6. System

Case sensitivity in java

 area. Area, Area, and AREA are four different


identifiers
 X is different than x
 Main is different than main
Tips

 Use clear and descriptive names


-> numberOfStudents, username
 Avoid using abbreviations
-> numStuds, uName
 Do not use the $ character when naming identifiers

You might also like