java

Subdecks (1)

Cards (286)

  • Object-Oriented Thinking
    A way of viewing the World
  • Agents and Communities

    An object-oriented program is structured as a community of interacting agents, called objects
  • Scenario
    • I (stays at Hyderabad) wants to send a flower bouquet to my friend, who is at Chennai
  • Objects
    • Each object has a role to play
    • Each object provides a service, or performs an action, that is used by other members of the community
  • Messages & Methods

    • Actions are initiated in object-oriented programming by the transmission of a message to an agent (an object) responsible for the action
    • The message encodes the request for an action
    • It is accompanied by any additional information (arguments) needed to carry out the request
    • The receiver is the object to whom the message is sent
    • In response to a message, the receiver will perform some method to satisfy the request
  • Responsibilities
    A fundamental concept in object-oriented programming is to describe the behavior in terms of responsibilities
  • Object
    • It is the basic unit of Object Oriented Programming and it represents the real life entities
    • Objects have two characteristics: they all have state and behavior
  • Examples of Objects

    • student
    • teacher
    • fan
    • dog
    • pen
  • State
    It is represented by attributes of an object
  • Behavior
    It is represented by methods of an object
  • Identity
    It gives a unique name to an object
  • Class
    • A class is a user defined blueprint or prototype (template) from which objects are created
    • It represents the set of properties or methods that are common to all objects of one type
    • It has definitions of methods and data
  • Data Hiding & Encapsulation
    • Using private access modifiers(access specifiers), we can hide data from other classes
    • Binding data and its operations together into a single unit is known as encapsulation
    • A java class is the example of encapsulation
  • Abstraction
    • Hiding internal details (implementation details) and showing functionality is known as abstraction
    • In Java, we use abstract class and interface to achieve abstraction
  • Inheritance
    • Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object
    • Existing class: Super Class / Base Class / Parent Class
    • New class: Sub Class / Derived Class / Child Class
    • The idea behind inheritance in Java is that you can create new classes that are built upon existing classes
    • When you inherit from an existing class, you can reuse methods and fields of the parent class
    • Moreover, you can add new methods and fields in your current class also
    • Inheritance represents the IS-A relationship which is also known as a parent-child relationship
  • Examples of Inheritance
    • Triangle IS-A Shape
    • Rectangle IS-A Shape
  • Why use inheritance in java

    • For Method Overriding (so runtime polymorphism can be achieved)
    • For Code Reusability
  • Polymorphism
    • Polymorphism is the ability of an object (methods) to take on many forms
    • Generally it occurs when we have many classes that are related to each other by inheritance
    • In Java, we use method overloading and method overriding to achieve polymorphism
  • Java Buzzwords/Features

    • Simple
    • Object Oriented
    • Platform Independent
    • Compiled & Interpreted but High performance
    • Architecture-neutral
    • Robust
    • Multithreaded
    • Distributed
    • Secure
  • Primitive Data Types

    boolean, byte, short, int, long, float, double, char
  • Variables
    • Variables are containers for storing data values
    • The value depends on the data type of the variable
    • The value can be altered during program exam execution
    • There are three types of variables in java: local, instance and static
  • Expressions
    Any unit of code that can be evaluated to a value is an expression
  • Operators
    • Unary Operators
    • Arithmetic Operators
    • Relational Operators
    • Equality Operators
    • Bitwise Operators
    • Logical Operators
    • Ternary Operator
    • Assignment Operators
  • Access Modifiers / Access Specifiers

    private, default, protected, public
  • Logical Operators (Logical Short Circuit Operators)

    &&, ||
  • Logical Operators
    • if(x != null && x.getValue() < 5) { // Do something }
  • Ternary Operator

    • int a=2; int b=5; int min=(a<b)?a:b; System.out.println(min);
  • Assignment Operators

    • int a=10; int b=20; a+=4; b-=4; System.out.println(a); System.out.println(b);
  • Access Modifiers / Access Specifiers

    They tell us the scope of the variable (where a variable can be accessed)
  • Access Modifiers

    • private, default, protected, public
  • Instance Data Members

    Each object has a separate copy of value for instance variables, they can be accessed with object only
  • Static Data Members

    There is only one copy of value in the memory for a given class, irrespective of how many objects the class has, all objects share this static variable, can be accessed with class name itself
  • Static Methods

    Their behavior is common to all objects, can be called directly with the class name, no need of object
  • Non-Static (Instance) Methods
    Operate on instance variables of the class, need an object to call
  • Wrapper Classes

    Byte, Short, Integer, Long, Float, Double, Character & Boolean, used for data type conversion
  • Command Line Arguments
    • class Test { public static void main(String args[]) { int x,y,z; x=Integer.parseInt(args[0]); y=Integer.parseInt(args[1]); z=x+y; System.out.println("The sum="+z); } }
  • Taking Input from User at Runtime (Scanner Class)
    • import java.util.Scanner; class ex3 { public static void main(String args[]) { int x,y,z; Scanner sc=new Scanner(System.in); System.out.println("Enter the first no:"); x=sc.nextInt(); System.out.println("Enter the second no:"); y=sc.nextInt(); if(x>y) System.out.println("First no is bigger"); else System.out.println("Second no is bigger"); } }
  • while Loop

    Executes a statement or code block repeatedly as long as an expression is true
  • while Loop

    • while (expression) { Statement(s) to be executed if expression is true }
  • do...while Loop

    Executes the statement(s) first, then checks the condition