class Allocate public static void main String args try long data new long

Class allocate public static void main string args

This preview shows page 26 - 31 out of 56 pages.

classAllocate { publicstaticvoidmain(String[] args) {
Background image
try{ longdata[] = newlong[1000000000]; } catch(Exception e) { System.out.println(e); } finally{ System.out.println("finally block will execute always."); } } } Output of program: finallyblock will execute always. Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at Allocate.main(Allocate.java:5) Exception occurred because we try to allocate a large amount of memory which is not available. Java program to swap two numbers This java program swaps two numbers using a temporary variable. To swap numbers without using extra variable see another code below. Swapping using temporary or third variable importjava.util.Scanner; classSwapNumbers { publicstaticvoidmain(String args[]) { intx, y, temp; System.out.println("Enter x and y"); Scanner in = newScanner(System.in); x = in.nextInt(); y = in.nextInt(); System.out.println("Before Swapping\nx = "+x+"\ny = "+y); temp = x; x = y; y = temp; System.out.println("After Swapping\nx = "+x+"\ny = "+y); } } Swap numbers program class file.
Background image
Output of program: Swapping without temporary variable importjava.util.Scanner; classSwapNumbers { publicstaticvoidmain(String args[]) { intx, y; System.out.println("Enter x and y"); Scanner in = newScanner(System.in); x = in.nextInt(); y = in.nextInt(); System.out.println("Before Swapping\nx = "+x+"\ny = "+y); x = x + y; y = x - y; x = x - y; System.out.println("After Swapping\nx = "+x+"\ny = "+y); } } For other methods to swap: C programming code to swap using bitwise XOR. Swapping is frequently used in sorting techniques such as bubble sort, quick sort etc. Java program to find largest of three numbers
Background image
This java program finds largest of three numbers and then prints it. If the entered numbers are unequal then "numbers are not distinct" is printed. Java programming source code importjava.util.Scanner; classLargestOfThreeNumbers { publicstaticvoidmain(String args[]) { intx, y, z; System.out.println("Enter three integers "); Scanner in = newScanner(System.in); x = in.nextInt(); y = in.nextInt(); z = in.nextInt(); if( x > y && x > z ) System.out.println("First number is largest."); elseif( y > x && y > z ) System.out.println("Second number is largest."); elseif( z > x && z > y ) System.out.println("Third number is largest."); elseSystem.out.println("Entered numbers are not distinct."); } } Download Largest of three numbers program class file. Output of program: If you want to find out largest of a list of numbers say 10 integers then using above approach is not easy, instead you can use array data structure. Enhanced for loop java
Background image
Enhanced for loop java: Enhanced for loop is useful when scanning the array instead of using for loop. Syntax of enhanced for loop is: for(data_type variable:array_name)Here array_name is the name of array. Java enhanced for loop integer array classEnhancedForLoop { publicstaticvoidmain(String[] args) { intprimes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; for(intt: primes) { System.out.println(t); } } } Download Enhanced for loop program.
Background image
Image of page 31

You've reached the end of your free preview.

Want to read all 56 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture