Lecture 5

221 views

Published on

Notes on core java

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
221
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lecture 5

  1. 1. Core Java Debasish Pratihari Type Casting:   Converting one type of data into another must follow the rules of casting. If a conversion results in the loss of precision, as in an int value converted to a short, then the compiler will issue an error message unless an explicit cast is made. a data type with lower precision (fewer bits) can be converted to a type of higher precision without explicit casting. To convert a higher precision type to a lower precision, however, an explicit cast is required or the compiler will flag an error. Primitive Type Conversion Table: Note :     The symbol C indicates that an explicit cast is required since the precision is decreasing. The symbol A indicates that the precision is increasing so an automatic cast occurs without the need for an explicit cast. N indicates that the conversion is not allowed. The * asterisk indicates that the least significant digits may be lost in the conversion. Lecture/core/LF2/05 Page #1 feel the Technology…
  2. 2. Core Java Debasish Pratihari && verses & :  The && operators perform Conditional-AND operations on two Boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.  The & operator do the same thing as && but it does not exhibit "short-circuiting" behavior, which means that the both operand are evaluated always. Enhanced for loop : Example:1 class LoopDemo{ public static void main(String[] args){ int[] numbers = {1,2,3,4,5,6,7,8,9,10}; for (int item : numbers) { System.out.println("Count is: " + item); } } } Example:2 class LoopDemo2{ public static void main(String[] args){ String [] names = {"Debasish","Sushant","Sidharth", "Sanat","Nayaran"}; for (String name : names) { System.out.println("Mr. " + name); } } } Controlling Statement in Java :    The break Statement o Un-labeled o Labeled The continue Statement o Un-labeled o Labeled The return Statement Lecture/core/LF2/05 Page #2 feel the Technology…
  3. 3. Core Java Debasish Pratihari Example of Un-labeled break : class BreakTest{ public static void main(String args[]){ for( int i=1;i<=5;i++){ System.out.println("Outer loop"+i); for(int j=10;j<=15;j++){ if(j==12) break ; System.out.println("Inner Loop"+j); } } } } Example of Labeled break : class BreakTest{ public static void main(String args[]){ stop: for( int i=1;i<=5;i++){ System.out.println("Outer loop"+i); for(int j=10;j<=15;j++){ if(j= =12) break stop; System.out.println("Inner Loop"+j); } } } } Example of Un-labeled continue : class ContinueTest{ public static void main(String args[]){ for( int i=1;i<=5;i++){ System.out.println("Outer loop"+i); for(int j=10;j<=15;j++){ if(j= =12) continue ; System.out.println("Inner Loop"+j); } } } } Lecture/core/LF2/05 Page #3 feel the Technology…
  4. 4. Core Java Debasish Pratihari Example of labeled continue : class ContinueTest{ public static void main(String args[]){ stop: for( int i=1;i<=5;i++){ System.out.println("Outer loop"+i); for(int j=10;j<=15;j++){ if(j==12) continue stop; System.out.println("Inner Loop"+j); } } } } Variable Scope : 25%  The Scope of a variable is the part of the program over which the variable name can be referenced. class x{ public static void main(String args[]){ int i; { int j; //referring to i and j is ok here } //referring to i ok here //referring to j is not ok here } } Assertion :  An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.  Each assertion contains a boolean expression that you believe will be true when the assertion executes. If it is not true, the system will throw an error.  The assertion statement has two forms. o assert Expression1 ; o assert Expression1 : Expression2 ; To activate assertions: o java –ea Test  Lecture/core/LF2/05 Page #4 feel the Technology…

×