




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































Run:
Run:
Run:
public class DataTypes002 { public static void main(String[] args) { // Overloading and underloading int c = 299792458; System. out .println("c = " + c); System. out .println("cc = " + cc); long lc = 299792458; System. out .println("lc = " + lc); System. out .println("lclc = " + lclc); System. out .println(); byte b = 120; for ( int i = 0; i < 10; i++) { b++; System. out .println(b); } System. out .println(); float fl = 1000000000000000000000000000000f; for ( int i = 0; i < 10; i++) { fl=10; System. out .println(fl); } System. out .println(); double db = Math. pow (10, 300); for ( int i = 0; i < 10; i++) { db=10; System. out .println(db); } System. out .println(); db = Math. pow (10, -320); for ( int i = 0; i < 5; i++) { db/=10; System. out .println(db); } } } c = 299792458 cc = - lc = 299792458 lclc = 89875517873681764 121 122 123 124 125 126 127
1.0E 1.0E 1.0000001E 1.00000004E 1.0E 1.00000004E 1.00000006E 1.0000001E Infinity Infinity 1.0E 1.0E 1.0E 1.0E 1.0E 9.999999999999999E 9.999999999999999E 9.999999999999998E Infinity Infinity 1.0E- 1.0E- 1.0E-
public class DataTypes003 { public static void main(String[] args) { // Characters char c = 't'; System. out .println(c); for ( int i = 0; i < 10; i++) { c++; System. out .println(c); } System. out .println(); System. out .println("ASCII Table"); c = 0; for ( int i = 1; i < 256; i++) { c++; System. out .print(c + " "); if (i % 10 == 0) System. out .println(); } System. out .println(); System. out .println(); c = '"'; System. out .println(c); c = '"'; System. out .println(c); c = '''; System. out .println(c); System. out .println("ASCII Table Again"); c = 0; for ( int i = 1; i < 256; i++) { c++; System. out .print(c + "\t"); if (i % 10 == 0) System. out .print("\n"); } } }
import java.util.Scanner; public class DataTypes004 { public static void main(String[] args) { // Reference Types Scanner keyboard = new Scanner(System. in ); System. out .print("Input an integer: "); int t = keyboard.nextInt(); /* Scanner keyboard2 = null; System.out.print("Input an second integer: "); int t2 = keyboard2.nextInt(); */ } } public class DataTypes005 { public static void main(String[] args) { // String Examples String str1 = "This is a test."; char ch1 = str1.charAt(0); System. out .println(ch1); System. out .println(str1.charAt(0)); System. out .println(str1.length()); System. out .println(); for ( int i = 0; i < str1.length(); i++) { System. out .println(str1.charAt(i)); } System. out .println(); String str2 = "this is a test."; System. out .println(str1.equals(str2)); System. out .println(str1.equalsIgnoreCase(str2)); System. out .println(); String str3 = String. valueOf (3.14159); System. out .println(str3); System. out .println(); System. out .println(str1.indexOf("i")); System. out .println(str1.indexOf("i", 3)); System. out .println(str1.indexOf("i", 7)); System. out .println(); System. out .println(str1.lastIndexOf("i")); System. out .println(str1.lastIndexOf("i", 3)); System. out .println(str1.lastIndexOf("i", 7)); System. out .println(str1.lastIndexOf("i", 1)); System. out .println(); System. out .println(str1.indexOf("is")); System. out .println(str1.indexOf("is", 3)); System. out .println(str1.indexOf("that")); System. out .println(); str1 = "cat"; str2 = "dog"; System. out .println(str1.compareTo(str2)); System. out .println(str1.compareTo(str1)); System. out .println(str2.compareTo(str1)); System. out .println(); str1 = "cat"; str2 = "Dog";
T h i s i s a t e s t. false true
2 5
5 2 5
2 5
0 1
System. out .println(str1.compareTo(str2)); System. out .println(str1.compareTo(str1)); System. out .println(str2.compareTo(str1)); System. out .println(); System. out .println(( int )('c') + " " + ( int )('D')); System. out .println(); System. out .println(str1.compareToIgnoreCase(str2)); System. out .println(str1.compareToIgnoreCase(str1)); System. out .println(str2.compareToIgnoreCase(str1)); System. out .println(); str1 = str1.concat(str2); System. out .println(str1); str1 = "cat"; str2 = "Dog"; str1 = str2 + str1; System. out .println(str1); System. out .println(); System. out .println(str1.endsWith("at")); System. out .println(str1.endsWith("ta")); System. out .println(); System. out .println(str1.startsWith("Dog")); System. out .println(str1.startsWith("dog")); System. out .println(); System. out .println(str1.isEmpty()); System. out .println(); System. out .println(str1); System. out .println(str1.toLowerCase()); System. out .println(str1.toUpperCase()); System. out .println(str1); str1 = str1.toUpperCase(); System. out .println(str1); System. out .println(); str1 = "This is a test string for testing the replace command."; System. out .println(str1); str1.replaceAll("test", "short"); System. out .println(str1.replaceAll("test", "short")); System. out .println(str1); System. out .println(str1.replaceFirst("test", "short")); System. out .println(str1); System. out .println(); System. out .println(str1.substring(5, 7)); System. out .println(str1.substring(15, 20)); System. out .println(str1.substring(15, 21)); System. out .println(str1.substring(15)); System. out .println(str1); System. out .println(); str1 = " This is a trim test "; System. out .println(str1.trim() + "*****"); } }
catDog Dogcat true false true false false Dogcat dogcat DOGCAT Dogcat DOGCAT This is a test string for testing the replace command. This is a short string for shorting the replace command. This is a test string for testing the replace command. This is a short string for testing the replace command. This is a test string for testing the replace command. is strin string string for testing the replace command. This is a test string for testing the replace command. This is a trim test*****
public class DataTypes008 { public static void main(String[] args) { // More random numbers for ( int i = 0; i < 10; i++) System. out .println(Math. random ()); for ( int i = 0; i < 10; i++) System. out .println(( int )(Math. random ()*7) + 5); } }
6 9 10 11 6 9 8 7 6 6