You are on page 1of 28

How to start programming in Java?

This part of the study material is not intended to address still concepts, but rather, you can configure any computer window xp or Windows 7 32 or 64 bit and get it ready to start programming. First of all: Installing "jdk-7u7-nb-7_2-windows-i586-ml" JDK "Java Development Kit", will not only Compile but run applications, available for a variety of operating systems: http://www.oracle.com/technetwork/java/javase/downloads/jdk7u7-downloads-1836413.html

METHOD 1: Use the Shell to run java code This point is important for you to read and performers very well using the following commands: javac.exe, compiler generated files *. Apartir class source code (*. Java). The *. Java is text, you can create and edit in a text editor, using the syntax of Java. Java.exe, systems interpreter for PC / Windows, executes bytecode files (files compiled class extension). The *. Class have executable code, bytecode, platform independent, to be executed on a "hypothetical or virtual machine" called Java Virtual Machine (JVM). Is this JVM neutral who plays this code making a particular code on the CPU used. This avoids having to make a different program for each CPU or plataforma.1 Shell enters the system and see what happens when Digitas commands: java or javac. You will realize that the javac command is not enabled, therefore the following steps and then try to use the command in question, the difference observed. Remember watching the second image of this manual and look in the directory where it is located javac command.

1 http://mmca13.blogspot.com/2008/09/compilar-and-execute-files-of-java.html To educate the system accepts this command indicating that we should add "environment variables" as indicated in the graph.

Creating our first program in java Install then an editor to start coding (in this case Notepad + +) After that, create the following program and save it in the directory "c: \ ProgramasJava" Note that the file name must be named identical to the class, using case sensitive.

Finally run the program, first compile the program with javac and then running the java program.

METHOD 2: Using the Eclipse IDE ENTERNO (Available at www.eclipse.org - Section "downloads" download the Eclipse Classic 4.2) This method is much faster than before, it is a method of abstraction for programming while we should not install the JDK, and load the necessary libraries to program without tread very rodeo. STEP 1: Download and unzip the file indicated

STEP 2: After that opening the Eclipse IDE, the program requests a folder where you will save the projects that we believe in java, proceed as indicated in the graph (you must create the folder previously).

STEP 3: Follow the following sequence of actions to create a project in java.

Look what happens next.

Now insert a class as follows

With the IDE, the only line of code you write play, which was noted below

Run the program with the button, watch out. Further notes what happened in "ProgramasJava2" you created. REFERENCES http://www.ajpdsoft.com/modules.php?name=news&file=print&sid=270

Step by step creation of the first program in Java using Eclipse


1. Run the sequence of actions detailed in the chart.

2. Run after the sequence of actions detailed in the chart.

3. Insert a class called "PruebaAlbum" as follows.

4. Now insert a class called "Album", in the same way as the previous step. 5. The result after the above steps is a structure as follows.

6. Declare the following attributes within the class "Album"

7. Then set the SET and GET methods (methods are inserting, modifying and extracting the values that variables may have happened before, "automatically generated"), this operation is called to create getters and setters.

8. Notice how you generated the following code block without you had to write something.

9. Define the following constructor. The constructor is a method that must have the same name as the class and is used to initialize the attributes of an object, usually receives values have past, a quick way is to develop a method using getters and setters the previous step.

10. Now we instantiate an object of class "Album" in class "PruebaAlbum", this instance or reference to create should be made in the main.

See how you created two instances, and the way in which you can send arguments. 11. In the previous step we created two instances, how can we show these values on screen? We must develop a new method in the "Album", will be called "ImprimirAlbum"

12. Then call the method created from the two instances declared in step 10.

13. And finally see the execution, after clicking on the button should appear something as follows:

BLOCK OF QUESTIONS

1.

Why does a class constructor is a special method?

Receive the same class name, and is a method that is called to create an instance of a class. 2. The methods getters and setters automatically created What methods help develop?

GETTERS methods help develop the constructor method, and the method Setters helps develop a method of printing an object's attributes.

INTRODUCTION TO JAVA CONCEPTS Java and all Java-based trademarks are trademarks of Sun Microsystems.1 What is Java JDK? JDK stands dejava Developers Kit is, toolset to develop (applications) in countless classes Java.Existen accompanying the JDK that is installed in order to get started programming in JAVA.

What makes it different from the other JAVA programming languages? What distinguishes Java from other programming languages is its conception of departure, in which language is to create a road that can be used to program in all types of operating systems and processors. One of the most important features is that the programs "executable", created by the Java compiler, are independent of the architecture. Run interchangeably in a variety of microprocessors and computers with different operating systems.

Is the language is compiled or interpreted? Java is compiled when the source code is translated into an object called machine code (binary, bytecode) and is interpreted because the machine code can be run on any platform which should be an interpreter executing it in real time. To run it, you need an interpreter, the JVM (JavaVirtual Machine) Java virtual machine. Thus, it is possible to compile the program on a UNIX workstation and run it on another Windows using the Java virtual machine for Windows. This JVM is responsible for reading the bytecodes and translate them into executable instructions directly on a particular microprocessor.

http://es.scribd .com/doc/3258260/98/La-clase-InputStream

The Java Virtual Machine (JVM). The Java virtual machine is the revolutionary idea of language. It is the entity that provides platform independence for Java programs compiled into byte-code.

BLOCK OF QUESTIONS What is the purpose of the JVM? Being a byte-code translator to make the same program in byte-code can be run on different platforms to transform the native machine code on which it runs.

What is a bytecode? It is an intermediate between the Java programming language and the final machine code. Why Java is not compiled into machine code, like other programs? Precisely to allow it to run on any computer on the Internet, it has a microprocessor 80x86, 680x0, PowerPC, Alpha, MIPS, etc ...

Bibliography http://es.scribd.com/doc/3258260/98/La InputStream classhttp://ingensis.blogspot.com/2010/06/java-is-a-language-compiled-e.html http://www.alegsa.com.ar/Diccionario/C/4133.php http://www.alegsa.com.ar/Diccionario/C/4133.php

The programmer must establish the association between the machine model "solution space" that is where to deploy the solution, such as a computer, and the problem is what you really want to solve "problem space" that is where the problem exists, the business. What is an abstraction? Alan kayresumion the five basic characteristics of Smalltalk for an object, the first object-oriented language that was successful and one of the languages that is based on JAVA. These characteristics represent a pure approach to object-oriented programming. 1. Everything is an object. Think of an object as a variable stores data, allows you "raise requests" asking him to perform operations on itself. In theory, you can take any conceptual component of the problem that is being addressed. 2. A program is a bunch of objects that tell each other what to do r sending messages. To make a solicitude to an object, you must send a message to that object. More specifically, you can think of a message is a request to call a method that belongs to a particular object. 3. Each object has its own memory made up of other objects. Stated another way, you can create a new class of Obeto defining a package containing existing objects. Therefore, it may increase the complexity of a program hiding behind the simplicity of the objects. 4. Every object has an associated type. As the saying goes, every object is an instance of a case where "class" synonymous with "type." The most important distinguishing characteristic of a class is "the set of messages that can be sent." 5. All objects of a particular type can receive the same messages. As discussed below, this statement is really important. Since an object of type "circle" is also an object of type "shape," a circle can be guaranteed to accept shape messages. This means that you can write code to communicate with objects of type form and automatically control anything that fits the description of a form. This capability is one of impersonation most important concepts of OOP. Booch offers an even more succinct description of object: - An object has state, behavior and identity. This means that an object can have internal data (which provides state), methods (to provide a behavior) and each object can be uniquely distinguished from other objects, ie, each object has a unique memory address . The object-oriented approach Reference is made to the elements in the problem space called "objects" to their representations in the solution space. The idea is that the program can adapt by itself to the lingo of the problem by adding new types of objects so that when you read the code describing the solution, you are reading words that also express the problem. Thus OOP allows you to describe the problem in terms of the problem rather than in terms of the computer. Why use classes?

Since a class describes a set of objects having characteristics (data elements) and behavior (functionality) identical actually a class is a type of data because, for example, a floating point number also has a set of characteristics and performance . The difference is that the programmer defines a class to fit a problem rather than force the use of an existing data type that was designed to represent a storage unit in a machine. You can extend the programming language by adding new data types specific to suit your needs. The programming system supports the new classes and provides all combinations of type providing defined types. Creating abstract data types (classes) is a fundamental concept in objectoriented programming. What is an object? Once a class has been defined, you can create as many objects of that class as desired and these objects can be handled as if the elements of the problem that is being addressed. But how do you make a object do useful work for the programmer? There must be a way to make an application in order to do something, such as complete a transaction, draw something on screen or a switch. What is an interface Also, each object can satisfy only certain requests. Requests can be made to an object are defined by its interface and is the type that determines the interface. An example with the representation of a light bulb:

The interface determines the applications that can be done to a certain object, so there must be a code somewhere to satisfy that request. This, along with the hidden data, define what it calls the implementation. From the standpoint of procedural programming, this is not complicated. One type has a method associated with each possible request, and when a specific request is made to a objesto, this method is called. This process is summarized by saying that the developer "send a message" (make a request) to an object and the object knows what to do with this message (it executes the code).

In this example, the name of the type / class is Light, the name of this particular object is lz Light and applications that can be done to an object light is on, off, or dim glow. It has created a light object defining a "reference" (lz) for that object and calling new to request a new object of that type. To send a message to the object, defined the object name and is related to the application of the message by a point. From the point of view of the user of a predefined class, that is the ultimate programming objects. The diagram above follows the format of UML (UnifiedModelingLanguage, Unified Modeling Language). Each class is represented by a box by typing the name of the guy at the top, the data members in the intermediate zone and methods (functions of said object receiving any message sent to the developer that object) in the bottom . Often in these diagrams only show the name of the class and public method, not including the buffer zone, as in this case. If you are only interested in the class name, it is not necessary to include the bottom.

There are a bunch of guys that are used very often in programming and requires special treatment. Types can be considered as "primitive", the reason for this special treatment is that to create a new object, a simple variable small, not efficient. To these primitive types, Java uses the technique used in C and C + +, that is, instead of creating the new variable creates a variable "automatic" is not a reference. Java determines the size of each primitive type. These sizes do not change from one machine architecture to another. This invariability of the sizes is one of the reasons of portability java.

Defaults primitive members When a primitive data type as a member of a class, Java guarantees that you will be assigned a default value if it fails to boot:

The defaults are only the values that Java guarantees when the variable is used as a class member. This ensures that the member variables of primitive type always be initialized (something C + + does not do), reducing a source of potential errors. However, this initial value may not be correct or even legal for the program you are writing. It is best to always initialize variables explicitly. This warranty does not apply to initialize local variables, those that are not class fields.

CONCEPTS OF OBJECT-ORIENTED PROGRAMMING (OOP) 1 What is OOP? Is to develop programs from objects. These objects waiting messages, process and send messages to other objects. In object-oriented programming, objects are defined that make up an application. These objects are formed by a series of features and operations that can be performed on them. 2 What is an object? The answer to this question in terms outside the programming seems simple. An object is a person, animal or thing. It is distinguished from other objects to have certain characteristics and is good for something, or put another way, you can perform various operations with or on that object. For example: A house is an object. FEATURES: Number of floors, Total height in meters, the facade color, number of windows, door number, city, street and number where it is located, etc.. OPERATIONS Build, destroy, painted facade, modify any of the characteristics, such as opening a new window, etc. Obviously, each object can be defined in terms of a multitude of features and an extensive array operations. Now in terms of programming, the programmer mission will determine what features and interest to maintain operations on an object. For example, on the home object may not be necessary to know its location and therefore, these features are not part of the object defined by the programmer. The same could be said about the operations. In terminology of object-oriented programming, the characteristics of the object are called attributes and operations METHODS. Each of these methods is a procedure or function belonging to an object. FORMAL INFORMAL TERMINOLOGY Terminology FEATURES ATTRIBUTES Operations (procedures and functions) METHODS

An object is formed by a number of features or data (attributes) and a set of operations (methods). No conceivable only on the basis of data or operations but as a whole. Example: Think about what methods would be needed to make a car turn Insert the key Turn the key This would trigger the electrical system The engine ignite 3. Classes and Objects In OOP we must distinguish between two closely related concepts, the class and object.

Analogous to how to define variables in a programming language, when you declare an object must define the type of object to which it belongs. This type is the class. In C, we define two variables X and Y integer as follows:

int X, Y; In this case, X and Y are variables and the type of these variables is integer. The way to declare objects in Java is the same: Ccasa casa1, house2; In this case, house2 casa1 and variables are effectively but somewhat special, are OBJECTS. Furthermore, the type of objects is Ccasa. This type is the class of the object Analogy Variable Object (X, Y) (casa1, house2)

Class Type (Int) (Ccasa)

Declaring casa1 and house2 as objects belonging to the class Ccasa, it indicates that house2 casa1 and have a number of attributes (data) and are nPuertas, nVentanas and color, and also a number of methods (operations that can be performed on them) as: abrirVentanas (), cerrarVentanas (), etc.. 4. Properties must meet to be considered a language object oriented. POTTING INHERITANCE POLYMORPHISM Encapsulation Encapsulation is the property possessed objects hide their attributes, and even methods to other parts of the program or other objects. The natural way to build a class is to define a set of attributes which generally are not accessible outside of the same object, but can only be modified by the methods which are defined as accessible from the outside of that class. Ccasa class { int nPuertas, nVentanas; String color; public Ccasa (int np, int nv, co String) { nPuertas = np; nVentanas = nv, color = co; }

public void paint (String co) { color = co; } public void abrirVentanas (int n) { nVentanas = nVentanas + n; } public void cerrarVentanas (int n) { nVentanas nVentanas =-n; if (nVentanas <0) nVentanas = 0; } public void abrirPuertas (int n) { nPuertas = nPuertas + n; } public void cerrarPuertas (int n) { nPuertas nPuertas =-n; if (nPuertas <0) nPuertas = 0; } } / / HERE STATEMENT Ccasa casa1 objects and instances, house2; The normal way to declare the class Ccasa is to define a set of attributes that are not accessible from anywhere in the program, but only through certain methods. So, if you open a new window in the house casa1, traditional philosophy of a programmer would do the following: casa1.N_VENTANAS casa1.N_VENTANAS = + 1; However, the natural way to do it in OOP is calling the method: casa1.abrirVentanas (1); That method (procedure) will increase by 1 nVentanas attribute. This does not mean that the attribute can not be accessed nVentanas the traditional way (if it had been defined as public, but for that language may be regarded as OOP, should allow for the possibility of prohibiting access to the attributes directly. Inheritance. One of the main advantages of OOP. This property allows you to define other descendant classes, so that the new class (the child class) inherits from the parent class all its attributes and methods. The new class can define new attributes and methods can even redefine existing attributes and methods (for example, change the type of an attribute or operations performed by a given method.) Is the natural way to define objects in real life. Most people would say, for example, that a house is a house with a garden. It has the same characteristics and properties or operations you can perform on a house and also incorporates a new feature, the garden. At other times, they added functionality (methods) and attributes. For example: a duck is a bird that nothing. It has the same characteristics as the birds and should only declare a method on the new class (the swim method).

This property enables code reuse and is very easy to take advantage of the existing class code, modifying them minimally to fit the new specifications. Example: Suppose we have constructed Ccasa class and we want to define a new class that represents the chalets. In this case you may want to define a new attribute that represents the square meters of garden. Instead of redefining a new class from scratch, can be used for writing code Ccasa class as follows.

As can be seen, only have to declare that the new class is a descendant of Ccasa Cchalet (extends Ccasa) and declares the new attribute.

Clearly, we must redefine constructor method to initialize the new attribute mJardin. But methods to open / close doors or windows is not necessary to define them are inherited from class Ccasa and can be used, for example as follows: chalet1.pintar ("White"); Polymorphism Polymorphism allows the same message to objects of different classes do these also behave differently (different objects can have methods with the same name or the same object methods can have identical names but different parameters) Ccasa class { public Ccasa (int np, int nv, co String) { nPuertas = np; nVentanas = nv, color = co; } public Ccasa () {nPuertas = 0; nVentanas = 0, color = ""; } } You have two methods with the same name but different parameters. In the first case, the attributes of the object initialized with the parameters of the method and in the second case will be initialized to zero, for example. Also, if you have two objects and chalet1 casa1 and calls the method: Bibliography http://es.scribd.com/doc/3258260/98/La-clase-InputStream

You might also like