1 / 26

UNIT II JAVA PROGRAMMING

Learn about constants in Java and how they are used to assign a fixed value to variable names, making code more robust and readable.

kemberly
Download Presentation

UNIT II JAVA PROGRAMMING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UNIT IIJAVA PROGRAMMING

  2. What is a Constant? • A constant in Java is used to map an exact and unchanging value to a variable name. • Constants are used in programming to make code a bit more robust and human readable importjava.io.File;   publicclass Images   {   publicstaticfinal String THUMBNAILS_DIRECTORY "; publicvoiddoSomethingWithThumbnails ()     {       File thumbnailFile = new File(THUMBNAILS_DIRECTORY);       // do something with thumbnailFile.....     }   }  

  3. 1) Local Variable A variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other methods in the class aren't even aware that the variable exists.Alocal variable cannot be defined with "static" keyword. 2) Instance Variable A variable declared inside the class but outside the body of the method, is called instance variable. It is not declared as static. It is called instance variable because its value is instance specific and is not shared among instances. 3) Static variable A variable which is declared as static is called static variable. It cannot be local. You can create a single copy of static variable and share among all the instances of the class. Memory allocation for static variable happens only once when the class is loaded in the memory.

  4. Example variables in java class Simple{   publicstaticvoid main(String[] args) { int a=10;   int b=10;   int c=a+b;   System.out.println(c);   } }

  5. Data Types in Java • Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java: • Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double. • Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays. • There are 8 types of primitive data types: • boolean data type • byte data type • char data type • short data type • int data type • long data type • float data type • double data type

  6. Java - Basic Operators • Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups • Arithmetic Operators • Relational Operators • Bitwise Operators • Logical Operators

  7. The Arithmetic Operators

  8. The Relational Operators

  9. The Bitwise Operators

  10. The Logical Operators

  11. Decision Making in Java (if, if-else, switch, break, continue, jump) Java’s Selection statements: • if • if-else • nested-if • if-else-if • switch-case • jump – break, continue, return

  12. if: if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not. Syntax: if(condition) { // Statements to execute if // condition is true }

  13. if-else: The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false. Syntax: if (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false }

  14. If…else

  15. nested-if: A nested if is an if statement that is the target of another if or else. Nested if statements means an if statement inside an if statement. Yes, java allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement. Syntax: if (condition1) { // Executes when condition1 is true if (condition2) { // Executes when condition2 is true } }

  16. nested-if:

  17. switch-case The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.Syntax: switch (expression) { case value1: statement1; break; case value2: statement2; break; case valueN: statementN; break; default: statementDefault; }

  18. switch-case

  19. else-if ladder • Syntax If(condition-1){StatementsStatements which will be executed if condition-1 is true}else if (condition-2){StatementsStatements which will be executed if condition-2 is true} ...else if (condition-n){StatementsStatements which will be executed if condition-n is true} else{StatementsStatements which will be executed if none of the conditions in condition-1, condition2,…condition-n are true.}

  20. Declaration of Class: • A class is declared by use of the class keyword. The class body is enclosed between curly braces { and }. The data or variables, defined within a class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class.

  21. Declaration of Class:

  22. Object in Java An entity that has state and behavior is known as an object e.g. chair, bike, marker, pen, table, car etc. It can be physical or logical (tangible and intangible). The example of an intangible object is the banking system. • An object has three characteristics: • State: represents the data (value) of an object. • Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc. • Identity: An object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.

More Related