background preloader

Srayy

Facebook Twitter

Lesson: Packages (The Java™ Tutorials > Learning the Java Language) Lesson: Numbers and Strings (The Java™ Tutorials > Learning the Java Language) Numbers This section begins with a discussion of the Number class (in the java.lang package) and its subclasses. In particular, this section talks about the situations where you would use instantiations of these classes rather than the primitive data types. Additionally, this section talks about other classes you might need to work with numbers, such as formatting or using mathematical functions to complement the operators built into the language. Finally, there is a discussion on autoboxing and unboxing, a compiler feature that simplifies your code. Strings Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language) With the knowledge you now have of the basics of the Java programming language, you can learn to write your own classes. In this lesson, you will find information about defining your own classes, including declaring member variables, methods, and constructors. You will learn to use your classes to create objects, and how to use the objects you create. This lesson also covers nesting classes within other classes, and enumerations Classes This section shows you the anatomy of a class, and how to declare fields, methods, and constructors.

Objects This section covers creating and using objects. More on Classes This section covers more aspects of classes that depend on using object references and the dot operator that you learned about in the preceding section: returning values from methods, the this keyword, class vs. instance members, and access control. Nested Classes Static nested classes, inner classes, anonymous inner classes, local classes, and lambda expressions are covered. Enum Types. Lesson: Language Basics (The Java™ Tutorials > Learning the Java Language) Variables You've already learned that objects store their state in fields. However, the Java programming language also uses the term "variable" as well. This section discusses this relationship, plus variable naming rules and conventions, basic data types (primitive types, character strings, and arrays), default values, and literals.

Operators This section describes the operators of the Java programming language. Expressions, Statements, and Blocks Operators may be used in building expressions, which compute values; expressions are the core components of statements; statements may be grouped into blocks. Control Flow Statements This section describes the control flow statements supported by the Java programming language. Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language) Lesson: Object-Oriented Programming Concepts (The Java™ Tutorials > Learning the Java Language) If you've never used an object-oriented programming language before, you'll need to learn a few basic concepts before you can begin writing any code. This lesson will introduce you to objects, classes, inheritance, interfaces, and packages.

Each discussion focuses on how these concepts relate to the real world, while simultaneously providing an introduction to the syntax of the Java programming language. What Is an Object? An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life.

This lesson explains how state and behavior are represented within an object, introduces the concept of data encapsulation, and explains the benefits of designing your software in this manner. What Is a Class? A class is a blueprint or prototype from which objects are created. What Is Inheritance? Inheritance provides a powerful and natural mechanism for organizing and structuring your software. What Is an Interface? Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials) Java Assignment Operators. Java Assignment Operators has average rating 8 out of 10. Total 3 users rated. <<PreviousNext>> Description Assigning a value to a variable seems straightforward enough; you simply assign the stuff on the right side of the '= 'to the variable on the left.

Below statement 1 assigning value 10 to variable x and statement 2 is creating String object called name and assigning value "Amit" to it. Statement 1: x =10; Statement 2: String name = new String ("Amit"); Assignment can be of various types. Ads Primitive Assignment : The equal (=) sign is used for assigning a value to a variable. Int x = 7; // literal assignment int y = x + 2; // assignment with an expression int z = x * y; // assignment with an expression with literal Primitive Casting Casting lets you convert primitive values from one type to another. Byte v = (byte) a; For cases where we try to assign smaller container variable to larger container variables we do not need of explicit casting.

Java Code Output Reference variable assignment. Java Arithmetic Operators. Java Arithmetic Operators has average rating 9 out of 10. Total 6 users rated. <<PreviousNext>> Description We can use arithmetic operators to perform calculations with values in programs. Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. A value used on either side of an operator is called an operand. For example, in below statement the expression 47 + 3, the numbers 47 and 3 are operands. Int a = 47+3; In Java, you need to be aware of the type of the result of a binary (two-argument) arithmetic operator. If either operand is of type double, the other is converted to double. For unary (single-argument) arithmetic operators: If the operand is of type byte, short, or char then the result is a value of type int. The basic arithmetic operations—addition, subtraction, multiplication, and division— all behave as you would expect for all numeric types.

The following simple program demonstrates the arithmetic operators. Java Code Output x++; x--; Java Conditional or Relational Operators. Java Conditional or Relational Operators has average rating 10 out of 10. Total 3 users rated. <<PreviousNext>> Description If you need to change the execution of the program based on a certain condition you can use “if” statements.

The relational operators determine the relationship that one operand has to the other. Specifically, they determine equality condition. Java provides six relational operators, which are listed in below table. The outcome of these operations is a boolean value. In Java, the simplest statement you can use to make a decision is the if statement. If(condition) statement; or if (condition) statement1; else statement2; Here, condition is a Boolean expression. If (someVariable ==10){ System.out.println("The Value is 10”); } if (someVariable ==10){ System.out.println("The Value is 10”); } We can have different flavors of if statements. Nested if blocks: A nested if is an if statement that is the target of another if or else. Java Code if –else if ladder: Output Summary. Java Logical Operators. Java Logical Operators has average rating 8 out of 10. Total 4 users rated. <<PreviousNext>> Description Sometimes, whether a statement is executed is determined by a combination of several conditions.You can use logical operators to combine these conditions.

Logical operators are known as Boolean operators or bitwise logical operators. The NOT Operator Also called the bitwise complement, the unary NOT operator, ~, inverts all of the bits of its operand. Int a =23; // 23 is represented in binary as 10111 int b = ~a; // this will reverts the bits 01000 which is 8 in decimal boolean x = true; boolean y = ! The AND Operator The AND operator “&” produces a 1 bit if both operands are 1 otherwise 0 bit. Int var1 = 23; //boolean value would be 010111 int var2 = 33; //boolean value would be 100001 int var3=var1 & var2 // result in binary 000001 & in decimal 1 boolean b1 = true; boolean b2=false; boolean b3 = b1&b2; // b3 would be false The OR Operator The XOR (exclusive OR) Operator Java Code Output. Java Arrays. Java Arrays has average rating 8 out of 10. Total 13 users rated. <<PreviousNext>> Description An array is a group of similar typed variables that are referred to by a common name.

Arrays of any type can be created and may have one or more dimensions. Defining and constructing one dimensional array Here, type specifies the type of variables (int, boolean, char, float etc) being stored, size specifies the number of elements in the array, and array-name is the variable name that is reference to the array. Initializing array: You can initialize specific element in the array by specifying its index within square brackets. ResultArray[0]=69; This will initialize first element (index zero) of resultArray[] with integer value 69. Array Literals The null literal used to represent the absence of an object can also be used to represent the absence of an array. String [] name = null; String[] daysOfWeek = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}; Output.

Java Wrapper Classes. Java Wrapper Classes has average rating 7 out of 10. Total 47 users rated. <<PreviousNext>> Description Each of Java's eight primitive data types has a class dedicated to it. The wrapper classes in java servers two primary purposes. To provide mechanism to ‘wrap’ primitive values in an object so that primitives can do activities reserved for the objects like being added to ArrayList, Hashset, HashMap etc. collection. The following two statements illustrate the difference between a primitive data type and an object of a wrapper class: int x = 25; Integer y = new Integer(33); The first statement declares an int variable named x and initializes it with the value 25. Below table lists wrapper classes in Java API with constructor details. Below is wrapper class hierarchy as per Java API As explain in above table all wrapper classes (except Character) take String as argument constructor. Integer intObj = new Integer (25); Integer intObj2 = new Integer ("25"); Integer intObj3 = new Integer ("Two");

Java Object Oriented Programming concepts. This tutorial will help you to understand about Java OOP’S concepts with examples. Let’s discuss what are the features of Object Oriented Programming. Writing object-oriented programs involves creating classes, creating objects from those classes, and creating applications, which are stand-alone executable programs that use those objects. A class is a template, blueprint,or contract that defines what an object’s data fields and methods will be. An object is an instance of a class. You can create many instances of a class. A Java class uses variables to define data fields and methods to define actions. Objects are made up of attributes and methods. There are three main features of OOPS. 1) Encapsulation 2) Inheritance 3) Polymorphism Let’s we discuss the features in details.

Encapsulation Encapsulation means putting together all the variables (instance variables) and the methods into a single unit called Class. Inheritance Polymorphism Output : Abstraction Summary Answer these questions: Inheritance (IS-A) vs. Composition (HAS-A) Relationship. Inheritance (ISA) vs. Composition (HASA) Relationship has average rating 7 out of 10.

Total 54 users rated. <<PreviousNext>> Description One of the advantages of Object-Oriented programming language is code reuse. There are two ways we can do code reuse either by implementation of inheritance (IS-A relationship), or object composition (HAS-A relationship). IS-A Relationship: In object oriented programming, the concept of IS-A is a totally based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance. It is key point to note that you can easily identify the IS-A relationship. HAS-A Relationship: Composition(HAS-A) simply mean use of instance variables that are references to other objects. Let’s understand these concepts with example of Car class. As shown above Car class has couple of instance variable and few methods. Class Maruti extends Car{ public void MarutiStartDemo(){ Engine MarutiEngine = new Engine(); MarutiEngine.start(); } }

Java Class, methods, instance variables. Java Class, methods, instance variables has average rating 7 out of 10. Total 24 users rated. <<PreviousNext>> Java Declaration and Access Modifiers All computer programs consist of two elements: code and data. Declaration of Class : A class is declared by use of the class keyword. Declaration of Instance Variables : Variables defined within a class are called instance variables because each instance of the class (that is, each object of the class) contains its own copy of these variables. Declaration of Methods : A method is a program module that contains a series of statements that carry out a task. Access modifiers: Each object has members (members can be variable and methods) which can be declared to have specific access. Public: Members (variables, methods and constructors) declared public (least restrictive) within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.

Below Table summarizes the access modifiers. Java Packages. Has average rating 8 out of 10. Total 7 users rated. <<PreviousNext>> Introduction A decently size project can have hundreds of Java classes and you need to organize them in packages (think file directories). Packagecom.sct.account; classSalesTax { // the class body goes here } packagecom.sct.account; classSalesTax { // the class body goes here } Java classes are also organized into packages and the fully qualified name of a class consists of the package name followed by the class name.

To use a public package member from outside its package, you must do one of the following: Refer to the member by its fully qualified name: Here no need of import statement but code will become non-readable due to longer statement. newpackageName.className().methodName( argument1, argument2); Import the package member: This way we can import one class from one package.

ImportpackageName.className; newclassName().methodName(argument 1, argument2); importpackageName newclassName().methodName(argument1, argument2); Java Development Environment Setup (JDK) Java Development Environment Setup (JDK) has average rating 8 out of 10. Total 2 users rated. <<PreviousNext>> Download and Install JDK, Eclipse (IDE) To develop and run any java program you need to install JDK in your system. You can download latest version of Java from here After selecting the Windows platform and clicking the Download button you’ll see a Login for Download screen, which is optional: You can skip this step. Once download completes you can start installation. You can recheck installation using command prompt. If you can’t see Java version as above on command prompt, you need to set class-path in system variable. Choose Start, Settings, Control Panel, and double-click System and select the Advanced tab and then click on Environment Variables (Figure 4).

The new path takes effect in each new Command Prompt window you open after setting the PATH variable. Running Java Program on Windows Step3: cd /tmp Summary. Compiling, running and debugging Java programs. Java SE Downloads. Java Tutorial. Java Primitive data type. Java Program Structure.