Posts

Taking Input from the user in Java

Image
Taking Input From User Three ways to accept the value dynamically From Cmd prompt From keyboard From properties file Cmd prompt:  Using main method passing the input values as array of String object (String args[]) Convert the received String objects using wrapper classes as follows:               public static xxx parsexxx(String).               Ex: To convert String to integer               int a = Integer.parseInt(args[0]); From Keyboard: Use Scanner class from java.util.scanner package Scanner s = new Scanner (System.in);                 int firstNumber = s.nextInt(); From properties file: Create file name db.properties and data as key=value format as follows: Read the properties file using FileReader and properties class in main method as follows:

Control Statement in Java

Control Statement The control statement are used to control the flow of execution of the program Types of control statements               Selection Repetition Branching Selection  Statements                                if-then, if-then-else and switch Repetition Statements                                while, do-while and for Branching statements                               break, continue and return

Constant in Java

Constant in Java  A constant in java is used to map and exact and unchanging value to a variable name. Final variables are often declared with static keyword in java and treated as constant . Final keyword in variable declaration: Syntax: final datatype v 1 = val 1 , v 2 =val 2, ----------------------V n = Val n ;              Ex: public static final double PI = 3.14159; Final keyword in method level: A java method with final keyword is called final method and it cannot be overridden in sub-class . Syntax:  final returntype methodname(list of formal parameters)                       {                                    block of statements;                        }               ...

Java Keywords List

Image
Java Keywords

Enums in Java

Enums in Java Enums were introduced in java 5.0 Enums restrict a variable to have one of only few predefined values. The values in this enumerated lists are called enums. With the use of enums it is possible to reduce the number of bugs in your code. For Example, if we consider and application for fresh juice shop, it would be possible to restrict the glass size to small, medium and large. This would make sure that it would not allow anyone to order any size other than the small, medium or large.

Java Modifiers

Java Modifiers Modifiers are keywords that you add to those definitions to change their meanings. Access Modifiers: public, protected, private, default Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are: Visible to package, the default. No modifiers are needed. Visible to class only (private). Visible to world (public). Visible to the package and all subclasses (protected). Non-access Modifiers: static, final, abstract, strictfp Java provides a number of non-access modifiers to achieve many other functionality. The static modifiers for creating class methods and variables. The final modifiers for finalizing the implementations of classes, methods and variables. The abstract modifier for creating abstract classes and methods. The synchronized and volatile modifiers, which are used for threads.

Data Types In Java

Image
Java Data Types Primitive data types: These data types allow variable to store only one value.     Ex: double salary = 25350.56; Non-primitive or derived data types: These data types allow the variables to store multiple value of the same data type.    Data Types:    Arrays            Ex: int a[] = {10,20,30,40,50}    Strings are enclosed in ""            Ex: String printval = "welcome"; User defined data type: These are developed by programmers and whose variables allows to store multiple values wither of the same type or different type or both. Ex: classes, interfaces.