Category: Java Core

Characters In Java

Understanding Through Code public class CharacterInJava { public static void main( String[] args ){ char charVal1 = '1'; char charVal2 = '2'; char charVal3 = '3'; System.out.println("charVal1: " +charVal1); System.out.println("charVal2: " +charVal2); System.out.println("charVal3: " +charVal3); char unicodeDollar = '\u0024'; System.out.println("unicodeDollar: " +unicodeDollar); char c1 = 'a'; char c2 = 'b'; System.out.print(Character.toUpperCase(c1)); //Note print is used not println System.out.println(Character.toUpperCase(c2)); } } Output charVal1: 1 charVal2: 2 charVal3: 3 unicodeDollar: $ AB Notes In Java character variable can be assigned many characters. Character is a primitive type. Literals for character are wrapped in single quotes ‘ ‘ You can assign character variable ... Read more

Boolean Values In Java

Understanding Through Code public class BooleanInJava { static boolean staticBool; //Remember primitives are assigned defaults public static void main( String[] args ){ boolean boolVal1 = true; boolean boolVal2 = false; System.out.println("boolVal1: " +boolVal1); System.out.println("boolVal2: " +boolVal2); System.out.println("staticBool: " +staticBool); boolean boolVal3 = !boolVal1; //negation of boolVal1 is assigned to boolVal3 System.out.println("boolVal3: " +boolVal3); int someInt = 1; boolean boolVal4 = (someInt != 1); System.out.println("boolVal4: " +boolVal4); String strBool = "false"; //this should be either true or false only boolean convertedBool = Boolean.parseBoolean(strBool); System.out.println("Parsing of boolean: " +convertedBool); } } Output boolVal1: true boolVal2: false staticBool: false boolVal3: false boolVal4: false ... Read more

Interchanging Numeric Values In Java

Try out the code public class ConvertNumericVal { public static void main( String[] args ){ int intVal = 30; int intVal1 = intVal; //We got two copies of the value System.out.println("intVal1 is: " +intVal1); long longVal = intVal1; System.out.println("longVal is: " +longVal); //short shortVal = intVal1; //ERROR! short shortVal = (short) intVal1; //typecasting -> narrowing the type System.out.println("shortVal is: " +shortVal); int intVal2 = 700; //byte byteVal = intVal2; //ERROR! byte byteVal = (byte) intVal2; //risk of losing data System.out.println("byteVal is: " +byteVal); double doubleVal = 1.91111d; int intVal3 = (int) doubleVal; //narrowing. Truncation results in loss of data. System.out.println("intVal3 ... Read more

Java Data Types

Java has 2 data types Primitives Objects Primitive Data Type Numbers, characters and booleans. Stored in fastest available memory — for faster access to data. Names given are all in lowercase — so you can distinguish them from complex objects. One exception data type that is not a primitive: String Declaring Primitive Variables Java is statically typed language unlike JavaScript or Python which are dynamic languages. All variables must have their types declared. Example: int aVar = 5; Example elaborated: int -> data type which stays fixed once assigned as Java allocated memory for you depending on the data type. ... Read more

Implementing Package in Java

What is a package? A package is a namespace concept which organizes a set of related interfaces and classes. It can be thought of as different folders on your system. Implementing Package package com.pratikkataria.java; public class Main { public static void main( String[] args) { System.out.println(args[0]); } } This Main.java file needs to be kept inside folders: com -> pratikkataria -> java -> Main.java So the command prompt needs to be started in the location where the com folder is: K:\> CONSOLE: K:\>javac com\pratikkataria\java\Main.java K:\>java com.pratikkataria.java.Main MyArg OUTPUT: MyArg

Passing Arguments To Console In Java

Remember String [] args ? This allows to pass values from console to Java application in the form of Strings. You can use those arguments within Java application by the name of array (here args) and index number public class Main { public static void main( String[] args) { System.out.println(args[0]); } } Console: K:\>javac Main.java K:\>java Main MyArgument OUTPUT: MyArgument Guess the output: Console: K:\>javac Main.java K:\>java Main My Argument OUTPUT ? My Argument WRONG! REAL OUTPUT: My Why? Java separates arguments by spaces. My -> args[0] Argument -> args[1] How to make your desired output work? Wrap your argument ... Read more

Java Memory Management and Garbage Collection

Automatic Memory Management Objects are allocated memory automatically unlike in C or C++ Objects are created in Heap Memory. Heap memory is a little slower than stack. But, heap is more dynamic. Complex objects are always stored in heap. If a variable references an object, it is retained i.e. it won’t be available for garbage collection. In other words, as long as a variable can be address in code, the object will be available. When all the references of object expire, it is eligible for garbage collection. However, garbage collection decides when to do that. Lifetime of References Local variables ... Read more

Java Basic Coding And Syntax

Java is defined in classes Each source file defines at least one java class that has the name same as given to the file with extension .java public class Firstcode { //File name will be: Firstcode.java } You can create java code in any text editor. However, sublime text or intellij is preferred. java command processes the bytecode which is generated by javac command //Package Declaration: package com.example; //Class Declaration: public class Firstcode { //Main Method: public static void main( String []args) { //Executable Code System.out.println("Hello World!"); } } Package: java classes are typically organized into packages. A package is ... Read more

Java Fundamentals

Principles of Java Simple Objected-Oriented Robust Methods, functions and properties Code in small chunks – easy to debug Portable High Performance JVM has improved Dynamic, interpreted and threaded Run-time interpretation Apps can do more than one thing at a time Compatibility between different data types Software Stack Operating System — Windows / Linux / Mac Java Virtual Machine (JVM) — Android / IBM / Sun microsystems Core runtime and additional libraries — JDK / JRE Compiled ByteCode Java  v/s  C++ Java C++ Not compatible with prev. languages Compatible with C Compiled to bytecode Compiled to native machine language Calls to ... Read more

Java Introduction

How Java was born? Java started in 1991 – when Sun Microsystems began ‘Green Project’ Primary objective: A new portable language – to run on multiple operating systems Original name: Oak – derived from large oak tree that stood outside the developer’s offices Renamed to: Java – probably because of the amount of coffee developers were drinking Public Access: 1995 Original motivation: Write Once, Run Everywhere – compile once and execute on any operating system Timeline 1995 – First Release 1996 – JDK 1.0 1997 – JDK 1.1 (Inner classes, JavaBeans, JDBC, RMI, Reflections) 1998 – J2SE 1.2 Swing graphical API ... Read more