|
|
(2 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
− | | + | #REDIRECT [[Object-Oriented Concepts and Constructs]] |
− | <br />
| |
− | ==Online Java Compiler==
| |
− | These ones supports many languages:
| |
− | | |
− | *https://repl.it/
| |
− | *https://www.onlinegdb.com
| |
− | | |
− | | |
− | <br />
| |
− | ==Installation==
| |
− | | |
− | | |
− | <br />
| |
− | ===Installing the Default OpenJDK===
| |
− | http://ubuntuhandbook.org/index.php/2018/11/how-to-install-oracle-java-11-in-ubuntu-18-04-18-10/
| |
− | | |
− | sudo apt update
| |
− |
| |
− | sudo apt install default-jdk
| |
− | | |
− | | |
− | <br />
| |
− | ===Oracle Java Development Kit JDK===
| |
− | https://en.wikipedia.org/wiki/Java_Development_Kit
| |
− | | |
− | https://www.oracle.com/technetwork/java/javase/overview/index.html
| |
− | | |
− | Es apropiado instalar '''Oracle JDK'''. Para GUI Programming, por ejemplo, se necesitan librerías que se encuentran en Oracle JDK.
| |
− | | |
− | Hay diferentes Edition:
| |
− | | |
− | *'''Java Platform, Standard Edition (Java SE)''' (ésta es la que generalmente necesitamos)
| |
− | *Java Platform, Enterprise Edition (Java EE)
| |
− | *Java Platform, Micro Edition (Java ME)
| |
− | *...
| |
− | | |
− | | |
− | <br />
| |
− | ====Installing Oracle JDK on Ubuntu====
| |
− | Installing Oracle JDK (Java EE) on Ubuntu 18.04:
| |
− | http://ubuntuhandbook.org/index.php/2018/11/how-to-install-oracle-java-11-in-ubuntu-18-04-18-10/
| |
− | | |
− | <span style="background:#D8BFD8">En una ocasion, al instalar '''java-10-oracle''' la instalación de NetBeans no funcionó correctamente. Esto creo que se debía a que Java-10-oracle era muy reciente para la fecha. Fue por tanto necesario instalar '''java-8-oracle''' y configurar la ruta del '''JDK''' editando el archivo '''/usr/local/netbeans-8.2/etc/netbeans.conf'''</span>
| |
− | | |
− | | |
− | <br />
| |
− | ====Installing Oracle JDK on Windows====
| |
− | Installing Oracle JDK (Java EE) on Windows:
| |
− | | |
− | *Descargar Java SE de la página oficial de Oracle:
| |
− | | |
− | ::https://www.oracle.com/technetwork/java/index.html
| |
− | ::https://www.oracle.com/technetwork/java/javase/downloads/index.html
| |
− | | |
− | *Instalación común sin ninguna remarca.
| |
− | *Los archivos se guardan generalmente by default en: C:\Program Files\Java
| |
− | **Cuando instalé NetBeans, éste detectó automáticamente la ruta del JDK - JDK for the NetBeans IDE:
| |
− | | |
− | ::C:\Program Files\Java\jdk1.8.0_181
| |
− | | |
− | | |
− | <br />
| |
− | ===Check your installed java version===
| |
− | java -version
| |
− | javac -version
| |
− | | |
− | | |
− | The fallowing command should tell you what is currently providing the Java virtual machine (java) and the Java compiler (javac): https://askubuntu.com/questions/150057/how-can-i-tell-what-version-of-java-i-have-installed
| |
− | file /etc/alternatives/java /etc/alternatives/javac
| |
− | | |
− | | |
− | <br />
| |
− | ==IDE for Java==
| |
− | | |
− | | |
− | <br />
| |
− | ===Eclipse===
| |
− | | |
− | | |
− | <br />
| |
− | ====Installing Eclipse====
| |
− | Para estar seguro de que estamos instalando la última versión, es apropiado descargar el paquete desde la página oficial, y no instalar la versión que se encuentra en los repositorios de Ubuntu a través de apt-get (pues estar versiones generalmente están desactualizadas).
| |
− | | |
− | Página oficial de Eclipse: http://www.eclipse.org/downloads/
| |
− | | |
− | En la siguiente página se explica como instalar eclipse en Ubuntu 16.04:
| |
− | http://ubuntuhandbook.org/index.php/2016/01/how-to-install-the-latest-eclipse-in-ubuntu-16-04-15-10/
| |
− | | |
− | Para instalarlo hay que simplemente ejecutar el instalador y seguir las instrucciones (como si se tratara de una instalación en Windows).
| |
− | | |
− | Luego, para agregar el ejecutable a la lista de comandos, he agregado la siguiente línea en el .bashrc:
| |
− | PATH=$PATH:/home/adelo/1-system/.1-conf-system/1-archivos_programas-ubuntu/eclipse/java-photon/eclipse:
| |
− | | |
− | | |
− | <br />
| |
− | ====Combinaciones de teclas====
| |
− | | |
− | *Para que coloque los import (luego de haber ingresado un comando que necesite una librería en particular): Ctrl Shif o
| |
− | *Run: Ctrl 11
| |
− | *Comentar: Ctrl 7
| |
− | *Rastrea la variable seleccionada: CTRL k
| |
− | | |
− | | |
− | <br />
| |
− | ===NetBeans===
| |
− | https://netbeans.org/
| |
− | | |
− | https://en.wikipedia.org/wiki/NetBeans
| |
− | | |
− | | |
− | <br />
| |
− | ====Installing NetBeans on Ubuntu 18.04====
| |
− | https://www.itzgeek.com/how-tos/linux/ubuntu-how-tos/how-to-install-netbeans-ide-on-ubuntu-18-04-ubuntu-17-10.html
| |
− | | |
− | Se puede descargar NetBeans manualmente de la página oficial: https://netbeans.org/downloads/
| |
− | | |
− | o a través de wget:
| |
− | <syntaxhighlight lang="bash">
| |
− | wget http://download.netbeans.org/netbeans/8.2/final/bundles/netbeans-8.2-linux.sh
| |
− | </syntaxhighlight>
| |
− | | |
− | Luego:
| |
− | <syntaxhighlight lang="bash">
| |
− | sudo bash netbeans-8.2-linux.sh
| |
− | </syntaxhighlight>
| |
− | | |
− | Durante la instalación es importante seleccionar el directorio en donde se encuentra Java, en mi caso:
| |
− | /usr/lib/jvm/java-8-oracle
| |
− | /usr/lib/jvm/java-10-oracle
| |
− | | |
− | <span style="background:#D8BFD8">Con '''java-10-oracle''' la instalación no funcionó correctamente. Fue por tanto necesario instalar '''java-8-oracle''' y configurar la ruta del '''JDK''' editando el archivo '''/usr/local/netbeans-8.2/etc/netbeans.conf'''</span>
| |
− | | |
− | | |
− | <br />
| |
− | =====Uninstall NetBeans=====
| |
− | https://askubuntu.com/questions/76908/how-to-uninstall-netbeans
| |
− | | |
− | Sólo hay que ejecutar el script '''uninstall.sh in /usr/local/netbeans-x.x'''
| |
− | bash uninstall.sh
| |
− | | |
− | | |
− | <br />
| |
− | ====Installing NetBeans on Windows 10====
| |
− | | |
− | *Descargar NetBeans de la página oficial: https://netbeans.org/downloads/
| |
− | *Cuando instalé NetBeans, éste detectó automáticamente la ruta del JDK - JDK for the NetBeans IDE:
| |
− | | |
− | ::C:\Program Files\Java\jdk1.8.0_181
| |
− | | |
− | *Debido a que instalé la versión que tiene todos los componentes, se instaló también el '''GlassFish Server Open Source Edition'''. No sé para que es sirve y si sea apropiado instalarlo.
| |
− | | |
− | | |
− | <br />
| |
− | ====The Darcula plugin====
| |
− | In NetBeans IDE 8.2 you can now go to:
| |
− | | |
− | *Tools > Plugins > Available Plugins > and install the '''Darcula LAF for NetBeans''' plugin directly from there.
| |
− | *En las versiones 9 y 10 el Plugin no se encuentra disponible por defecto. Es entonces necesario adicionarlo a la lista de "Available Plugins" de la siguiente forma:
| |
− | | |
− | ::https://stackoverflow.com/questions/52688439/changing-theme-in-apache-netbeans-9-0
| |
− | | |
− | | |
− | <br />
| |
− | | |
− | ==Using Java==
| |
− | | |
− | | |
− | <br />
| |
− | ===Writing and Running a Java code===
| |
− | You can use your favorite Text Editor or IDE to write your Python code:
| |
− | | |
− | Main.java
| |
− | <syntaxhighlight lang="java">
| |
− | public class Main
| |
− | {
| |
− | public static void main(String[] args) {
| |
− | System.out.println("Hello World");
| |
− | }
| |
− | }
| |
− | </syntaxhighlight>
| |
− | | |
− | | |
− | '''Using an IDE:'''
| |
− | | |
− | *You can run the program using the GUI options available in your IDE; which will usually compile, run and show the result in the IDE console.
| |
− | | |
− | | |
− | '''Through the terminal:'''
| |
− | | |
− | *To compile the file, open your terminal and type:
| |
− | <blockquote>
| |
− | <syntaxhighlight lang="shell">
| |
− | javac filename.java
| |
− | </syntaxhighlight>
| |
− | </blockquote>
| |
− | | |
− | *To run the generated .class file, use:
| |
− | <blockquote>
| |
− | <syntaxhighlight lang="shell">
| |
− | java filename
| |
− | </syntaxhighlight>
| |
− | </blockquote>
| |
− | | |
− | | |
− | <br />
| |
− | | |
− | ==Jar File==
| |
− | A Jar File (acronym for Java Archive) is format used for archiving java files (merging several java files into one)
| |
− | | |
− | The Jar File, provides a method to package our software to give it to the users.
| |
− | | |
− | The Jar File can be Runnable. That is, when we double click it, our program will run.
| |
− | | |
− | | |
− | A typical extension to files that we have always seen when working in a Windows environment is the .exe file extension. These files are a single binary executable file that when double clicked will open the program for the user.
| |
− | | |
− | In the Java world however things are a little different. There is no native .exe file in Java and we must use the .jar file extension instead. A jar file works exactly the same as a .exe file and when double clicked will run the program.
| |
− | | |
− | '''Jar files come in two different varieties:'''
| |
− | | |
− | *Simple archive folder
| |
− | *Runnable archive folder
| |
− | | |
− | A jar file is an acronym for Java Archive. Simple archive folder is a filetype similar to that of the .zip extension. If we want, we can open up a jar file using a program such as WinZip or WinRar and check the contents inside of the jar.
| |
− | | |
− | The second variant of this is the runnable archive folder. This type of file is exactly the same as the archive folder, just with one additional file. This extra file is used to define what happens when the user double clicks on the icon. In our case, we will point to the main method in our class file that we want to run. This will kick start the program to run when it is double clicked.
| |
− | | |
− | | |
− | <br />
| |
− | ===Creating a Jar===
| |
− | Open up your Eclipse and navigate to the project you are currently working on. Note this will only work correctly with an application that has a GUI for the user to see. In this case the project we are looking at is called Sample Program 1.
| |
− | | |
− | *Right click on the project name, and a menu will open.
| |
− | | |
− | *In the menu that opens, click on the Export button.
| |
− | | |
− | *In the window that opens you will see that two different types of JAR file are available:
| |
− | **If you wanted to simply package your project to give to someone, you could use the '''JAR file.'''
| |
− | **In our case, we are interested in the '''Runnable JAR file''', so we will click on this option and then click Next.
| |
− | | |
− | *You will then be passed to a second window where you will be able to specify the details for the runnable Jar file you are about to create:
| |
− | **The first of these is the '''launch configuration'''. This is the most important step to ensure that your Jar file will open the project when it is double clicked.
| |
− | ***Select the name of the class you want to run, inside of the project that you have been working on. In this case, you can see that the Main class is selected which is inside of the project titled SampleProgram1 that we are working in.
| |
− | **The second step is then to select a location where we would like to export the Jar file to.
| |
− | **After you have finished, click Finish at the bottom of the page.
| |
− | | |
− | *A small warning message may pop up, but this is ok. Click Ok to ignore this message.
| |
− | | |
− | | |
− | <br />
| |
− | ====Include images into the jar====
| |
− | https://coderanch.com/t/343401/java/pictures-loaded-executable-jar-file
| |
− | | |
− | To make sure that images used in the GUI are shown in the .jar, the images in the code have to be manages this way:
| |
− | | |
− | | |
− | <syntaxhighlight lang="java">
| |
− | .
| |
− | .
| |
− | -
| |
− | private void setAnimalPanel() {
| |
− | animalPanel = new JPanel(null);
| |
− | animalPanel.setLayout(null);
| |
− | animalPanel.setBorder(BorderFactory.createTitledBorder(null, "Animals: ", 0, 0, new Font("PLAIN", Font.BOLD, MainFrame.xCoordinate(12))));
| |
− | animalPanel.setBounds(MainFrame.xCoordinate(220), MainFrame.yCoordinate(70), MainFrame.xCoordinate(200), MainFrame.yCoordinate(185));
| |
− |
| |
− | JLabel animalImage = new JLabel();
| |
− | ImageIcon imgA = new ImageIcon(this.getClass().getResource("/images/animals.png"));
| |
− |
| |
− | Image aImg = imgA.getImage();
| |
− | Image newImgA = aImg.getScaledInstance(MainFrame.xCoordinate(90), MainFrame.yCoordinate(90), Image.SCALE_SMOOTH);
| |
− | ImageIcon newIconA = new ImageIcon(newImgA);
| |
− | animalImage.setIcon(newIconA);
| |
− | animalImage.setPreferredSize(new Dimension(MainFrame.xCoordinate(90), MainFrame.yCoordinate(90)));
| |
− | animalImage.setBounds(MainFrame.xCoordinate(0), MainFrame.yCoordinate(30), MainFrame.xCoordinate(100), MainFrame.yCoordinate(100));
| |
− | animalPanel.add(animalImage);
| |
− | }
| |
− | .
| |
− | .
| |
− | -
| |
− | </syntaxhighlight>
| |
− | | |
− | | |
− | See how we did it in the project '''''CCTZoo'''''
| |
− | | |
− | | |
− | <br />
| |
− | ===Exploring the Jar File===
| |
− | Now that we have created the Jar file. If we double click on the file our program will run. However if we want, we can take a look inside of the Jar file. This is easily done by right clicking on the Jar file that we have created and clicking “Open with WinRAR” or whichever program you currently have installed on your system for viewing Zip files.
| |
− | | |
− | After WinRAR has opened you will be able to see the contents of the Jar file. In this case you can see the class file from the project that we have been developing. In addition to this, you will see an additional folder titled '''META-INF'''. This is the folder where all of the configuration for the Jar file are held. We will go into this folder and take a look around.
| |
− | | |
− | Inside of this folder, you will see a file titled '''MANIFEST.MF'''. This is the main configuration file for the Jar file we have created. If we open up this file, we can see the details of what is called when we double click on the Jar file to view it.
| |
− | | |
− | There is very little content that is needed to make the Jar file run. Inside of this file, you can see that the Main-Class attribute is set to the name of the class file that we want to run when the Jar is double clicked. In the case of this project, the class file that we want to run is called '''Main.class''', so the title Main is used as the value for this attribute. If your class was called MyProgram.class, this would be set to MyProgram.
| |
− | | |
− | | |
− | <br />
| |
− | | |
− | ==Data types==
| |
− | https://www.javatpoint.com/java-data-types
| |
− | | |
− | | |
− | [[File:Java_data_types.png|511x511px|thumb|center]]
| |
− | | |
− | | |
− | <br />
| |
− | ===Primitive data types===
| |
− | There are 8 types of primitive data types:
| |
− | | |
− | {| class="wikitable" style="margin: 0 auto;"
| |
− | |+
| |
− | !
| |
− | !'''Data Type'''
| |
− | !'''Default Value'''
| |
− | !'''Size'''
| |
− | !Description
| |
− | ! colspan="2" |Example
| |
− | |-
| |
− | | rowspan="6" |'''Numeric'''
| |
− | |byte
| |
− | |0
| |
− | |1 byte
| |
− | |Stores whole numbers from -128 to 127
| |
− | | colspan="2" |<code>byte myNum = 100;
| |
− | System.out.println(myNum);</code>
| |
− | |-
| |
− | |short
| |
− | |0
| |
− | |2 byte
| |
− | |Stores whole numbers from -32,768 to 32,767
| |
− | | colspan="2" |<code>short myNum = 5000;</code>
| |
− | |-
| |
− | |int
| |
− | |0
| |
− | |4 byte
| |
− | |Stores whole numbers from -2,147,483,648 to 2,147,483,647
| |
− | | colspan="2" |<code>int myNum = 100000;</code>
| |
− | |-
| |
− | |long
| |
− | |0L
| |
− | |8 byte
| |
− | |Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
| |
− | | colspan="2" |<code>long myNum = 15000000000L;</code>
| |
− | |-
| |
− | |float
| |
− | |0.0f
| |
− | |4 byte
| |
− | |Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits.
| |
− | | |
− | The <code>float</code> data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an "f":
| |
− | |<code>float myNum = 5.75f;</code>
| |
− | | rowspan="2" |'''Scientific Numbers'''
| |
− | | |
− | A floating point number can also be a scientific number with an "e" to indicate the power of 10:
| |
− | | |
− | | |
− | <code>float f1 = 35e3f;</code>
| |
− | | |
− | <code>double d1 = 12E4d;</code>
| |
− | |-
| |
− | |double
| |
− | |0.0d
| |
− | |8 byte
| |
− | |Stores fractional numbers. Sufficient for storing 15 decimal digits.
| |
− | | |
− | The <code>double</code> data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value with a "d"
| |
− | |<code>double myNum = 19.99d;</code>
| |
− | |-
| |
− | |'''Character'''
| |
− | |char
| |
− | |'\u0000'
| |
− | |2 byte
| |
− | |The <code>char</code> data type is used to store a '''single''' character. The character must be surrounded by single quotes, like 'A' or 'c':
| |
− | | colspan="2" |<code>char myGrade = 'B';</code>
| |
− | | |
− | | |
− | Alternatively, you can use ASCII values to display certain characters:
| |
− | | |
− | <code>char a = 65, b = 66, c = 67;</code>
| |
− | | |
− | '''Tip:''' A list of all ASCII values can be found at https://www.w3schools.com/charsets/ref_html_ascii.asp
| |
− | |-
| |
− | |'''Boolean'''
| |
− | |boolean
| |
− | |false
| |
− | |1 bit
| |
− | |Stores true or false values
| |
− | | colspan="2" |<code>boolean isJavaFun = true;</code>
| |
− | |}
| |
− | | |
− | | |
− | <br />
| |
− | ===Collections===
| |
− | https://www.javatpoint.com/collections-in-java
| |
− | | |
− | [[File:Java_collections.png|647x647px|thumb|center|https://www.javatpoint.com/collections-in-java]]
| |
− | | |
− | | |
− | {| class="wikitable"
| |
− | |+
| |
− | !Collection
| |
− | !Description
| |
− | !Example
| |
− | |-
| |
− | |'''Arrays''' ''class''
| |
− | |https://www.w3schools.com/java/java_arrays.asp
| |
− | | |
− | | |
− | Arrays are used to store multiple values in a single variable.
| |
− | | |
− | To declare an array, define the variable type with '''square brackets'''.<br />
| |
− | |'''Array initialisation:'''
| |
− | <code>String[] cars;</code>
| |
− | | |
− | <code>String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};</code>
| |
− | | |
− | <code>int[] myNum = {10, 20, 30, 40};</code>
| |
− | | |
− | | |
− | '''Access the Elements of an Array:'''
| |
− | | |
− | <code>System.out.println(cars[0]);</code>
| |
− | | |
− | | |
− | '''Change an Array Element''':
| |
− | | |
− | <code>cars[0] = "Opel";</code>
| |
− | | |
− | | |
− | '''Array Length''':
| |
− | | |
− | <code>System.out.println(cars.length);</code>
| |
− | | |
− | '''Loop Through an Array''':<syntaxhighlight lang="java">
| |
− | String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
| |
− | | |
− | for (int i = 0; i < cars.length; i++) {
| |
− | System.out.println(cars[i]);
| |
− | }
| |
− | </syntaxhighlight>
| |
− | | |
− | '''Loop Through an Array with For-Each''':<syntaxhighlight lang="java">
| |
− | String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
| |
− | | |
− | for (String i : cars) {
| |
− | System.out.println(i);
| |
− | }
| |
− | </syntaxhighlight>
| |
− | | |
− | '''Multidimensional Arrays''':<syntaxhighlight lang="java">
| |
− | int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
| |
− | | |
− | // This is the representation of the myNumbers array.
| |
− | // Notice that it doesn't have to be symmetric (rectangular)
| |
− | 1 2 3 4
| |
− | 5 6 7
| |
− | | |
− | int x = myNumbers[1][2];
| |
− | System.out.println(x);
| |
− | // Outputs: 7
| |
− | </syntaxhighlight>
| |
− | |-
| |
− | |'''ArrayList''' ''class''
| |
− | |https://www.javatpoint.com/java-arraylist
| |
− | | |
− | | |
− | Java ArrayList class uses a dynamic array for storing the elements.
| |
− | | |
− | | |
− | The important points about Java ArrayList class are:
| |
− | | |
− | *Java ArrayList class can contain duplicate elements.
| |
− | *Java ArrayList class maintains insertion order.
| |
− | *Java ArrayList class is non synchronized.
| |
− | *Java ArrayList allows random access because array works at the index basis.
| |
− | *In Java ArrayList class, manipulation is slow because a lot of shifting needs to occur if any element is removed from the array list.
| |
− | |<syntaxhighlight lang="java">
| |
− | import java.util.*;
| |
− | | |
− | class ArrayList1{
| |
− | public static void main(String args[]){
| |
− | ArrayList<String> list=new ArrayList<String>();
| |
− | list.add("Ravi");//Adding object in arraylist
| |
− | list.add("Vijay");
| |
− | list.add("Ravi");
| |
− | list.add("Ajay");
| |
− | | |
− | //Invoking arraylist object
| |
− | System.out.println(list);
| |
− | }
| |
− | }
| |
− | </syntaxhighlight>
| |
− | |-
| |
− | |'''LinkedList''' ''class''
| |
− | |https://www.javatpoint.com/java-linkedlist
| |
− | | |
− | | |
− | Java LinkedList class uses a doubly linked list to store the elements. It provides a linked-list data structure.
| |
− | | |
− | | |
− | | |
− | The important points about Java LinkedList are:
| |
− | | |
− | *Java LinkedList class can contain duplicate elements.
| |
− | *Java LinkedList class maintains insertion order.
| |
− | *Java LinkedList class is non synchronized.
| |
− | *In Java LinkedList class, manipulation is fast because no shifting needs to occur.
| |
− | *Java LinkedList class can be used as a list, stack or queue.
| |
− | |<syntaxhighlight lang="java">
| |
− | import java.util.*;
| |
− | | |
− | public class LinkedList1{
| |
− | public static void main(String args[]){
| |
− | LinkedList<String> al=new LinkedList<String>();
| |
− | al.add("Ravi");
| |
− | al.add("Vijay");
| |
− | al.add("Ravi");
| |
− | al.add("Ajay");
| |
− | | |
− | Iterator<String> itr=al.iterator();
| |
− | while(itr.hasNext()){
| |
− | System.out.println(itr.next());
| |
− | }
| |
− | }
| |
− | </syntaxhighlight>
| |
− | |-
| |
− | |'''Queue''' ''Interface''
| |
− | | |
− | | |
− | '''PriorityQueue''' ''class''
| |
− | |https://www.javatpoint.com/java-priorityqueue
| |
− | | |
− | | |
− | '''Java Queue Interface'''
| |
− | | |
− | Java Queue interface orders the element in FIFO (First In First Out) manner. In FIFO, first element is removed first and last element is removed at last.
| |
− | | |
− | | |
− | '''PriorityQueue class'''
| |
− | | |
− | The PriorityQueue class provides the facility of using queue. But it does not orders the elements in FIFO manner. It inherits AbstractQueue class.
| |
− | |<syntaxhighlight lang="java">
| |
− | import java.util.*;
| |
− | | |
− | class TestCollection12{
| |
− | public static void main(String args[]){
| |
− | | |
− | PriorityQueue<String> queue=new PriorityQueue<String>();
| |
− | queue.add("Amit");
| |
− | queue.add("Vijay");
| |
− | queue.add("Karan");
| |
− | queue.add("Jai");
| |
− | queue.add("Rahul");
| |
− | System.out.println("head:"+queue.element());
| |
− | System.out.println("head:"+queue.peek());
| |
− | System.out.println("iterating the queue elements:");
| |
− | Iterator itr=queue.iterator();
| |
− | | |
− | while(itr.hasNext()){
| |
− | System.out.println(itr.next());
| |
− | }
| |
− | | |
− | queue.remove();
| |
− | queue.poll();
| |
− | System.out.println("after removing two elements:");
| |
− | Iterator<String> itr2=queue.iterator();
| |
− | | |
− | while(itr2.hasNext()){
| |
− | System.out.println(itr2.next());
| |
− | }
| |
− | }
| |
− | }
| |
− | </syntaxhighlight>
| |
− | |}
| |
− | | |
− | | |
− | <br />
| |
− | | |
− | ==Operators==
| |
− | https://www.w3schools.com/java/java_operators.asp
| |
− | | |
− | https://www.javatpoint.com/operators-in-java<section begin="operators" />
| |
− | {| class="wikitable"
| |
− | |+
| |
− | ! rowspan="2" |
| |
− | ! colspan="4" |Operator
| |
− | ! rowspan="2" |Name/Description
| |
− | ! colspan="4" |Example
| |
− | ! rowspan="2" |Same as
| |
− | |-
| |
− | ![https://www.w3schools.com/python/python_operators.asp Python]
| |
− | [https://www.tutorialspoint.com/python/python_basic_operators.htm s2]
| |
− | ![https://www.w3schools.com/java/java_operators.asp Java]
| |
− | !JS
| |
− | !R
| |
− | !Python
| |
− | !Java
| |
− | !F
| |
− | !R
| |
− | |-
| |
− | ! rowspan="9" |'''Arithmetic Operators'''
| |
− | |<nowiki>+</nowiki>
| |
− | |<nowiki>+</nowiki>
| |
− | |
| |
− | |
| |
− | |Addition
| |
− | |x + y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |<nowiki>-</nowiki>
| |
− | |<nowiki>-</nowiki>
| |
− | |
| |
− | |
| |
− | |Subtraction
| |
− | |x - y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |*
| |
− | |*
| |
− | |
| |
− | |
| |
− | |Multiplication
| |
− | |x * y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |/
| |
− | |/
| |
− | |
| |
− | |
| |
− | |Division
| |
− | |x / y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |%
| |
− | |%
| |
− | |
| |
− | |
| |
− | |Modulus
| |
− | |x % y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |**
| |
− | |<code>java.util.Math</code>
| |
− | |
| |
− | |
| |
− | |Exponentiation
| |
− | |x ** y
| |
− | |<code>import java.util.Math</code>
| |
− | | |
− | <code>Double result = Math.pow(number, exponent);</code>
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |//
| |
− | |
| |
− | |
| |
− | |
| |
− | |Floor division
| |
− | |x // y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |
| |
− | |<nowiki>++</nowiki>
| |
− | |
| |
− | |
| |
− | |'''Increment''': Increases the value of a variable by 1
| |
− | |<nowiki>++x</nowiki>
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |
| |
− | |<nowiki>--</nowiki>
| |
− | |
| |
− | |
| |
− | |'''Decrement:''' Decreases the value of a variable by 1
| |
− | |<nowiki>--x</nowiki>
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | ! rowspan="13" |'''Assignment Operators'''
| |
− | |=
| |
− | |=
| |
− | |
| |
− | |
| |
− | |
| |
− | |x = 5
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |<nowiki>+=</nowiki>
| |
− | |<nowiki>+=</nowiki>
| |
− | |
| |
− | |
| |
− | |
| |
− | |x+= 3
| |
− | |
| |
− | |
| |
− | |
| |
− | |x = x + 3
| |
− | |-
| |
− | |<nowiki>-=</nowiki>
| |
− | |<nowiki>-=</nowiki>
| |
− | |
| |
− | |
| |
− | |
| |
− | |x -= 3
| |
− | |
| |
− | |
| |
− | |
| |
− | |x = x - 3
| |
− | |-
| |
− | |*=
| |
− | |*=
| |
− | |
| |
− | |
| |
− | |
| |
− | |x *= 3
| |
− | |
| |
− | |
| |
− | |
| |
− | |x = x * 3
| |
− | |-
| |
− | |/=
| |
− | |/=
| |
− | |
| |
− | |
| |
− | |
| |
− | |x /= 3
| |
− | |
| |
− | |
| |
− | |
| |
− | |x = x / 3
| |
− | |-
| |
− | |%=
| |
− | |%=
| |
− | |
| |
− | |
| |
− | |
| |
− | |x %= 3
| |
− | |
| |
− | |
| |
− | |
| |
− | |x = x % 3
| |
− | |-
| |
− | |//=
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |x //= 3
| |
− | |
| |
− | |
| |
− | |
| |
− | |x = x // 3
| |
− | |-
| |
− | |**=
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |x **= 3
| |
− | |
| |
− | |
| |
− | |
| |
− | |x = x ** 3
| |
− | |-
| |
− | |&=
| |
− | |&=
| |
− | |
| |
− | |
| |
− | |
| |
− | |x &= 3
| |
− | |
| |
− | |
| |
− | |
| |
− | |x = x & 3
| |
− | |-
| |
− | |<nowiki>|=</nowiki>
| |
− | |<nowiki>|=</nowiki>
| |
− | |
| |
− | |
| |
− | |
| |
− | |<nowiki>x |= 3</nowiki>
| |
− | |
| |
− | |
| |
− | |
| |
− | |<nowiki>x = x | 3</nowiki>
| |
− | |-
| |
− | |^=
| |
− | |^=
| |
− | |
| |
− | |
| |
− | |
| |
− | |x ^= 3
| |
− | |
| |
− | |
| |
− | |
| |
− | |x = x ^ 3
| |
− | |-
| |
− | |>>=
| |
− | |>>=
| |
− | |
| |
− | |
| |
− | |
| |
− | |x >>= 3
| |
− | |
| |
− | |
| |
− | |
| |
− | |x = x >> 3
| |
− | |-
| |
− | |<<=
| |
− | |<<=
| |
− | |
| |
− | |
| |
− | |
| |
− | |x <<= 3
| |
− | |
| |
− | |
| |
− | |
| |
− | |x = x << 3
| |
− | |-
| |
− | ! rowspan="6" |'''Comparison Operators'''
| |
− | |==
| |
− | |==
| |
− | |
| |
− | |
| |
− | |Equal
| |
− | |x == y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |!=
| |
− | |!=
| |
− | |
| |
− | |
| |
− | |Not equal
| |
− | |x != y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |>
| |
− | |>
| |
− | |
| |
− | |
| |
− | |Greater than
| |
− | |x > y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |<
| |
− | |<
| |
− | |
| |
− | |
| |
− | |Less than
| |
− | |x < y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |>=
| |
− | |>=
| |
− | |
| |
− | |
| |
− | |Greater than or equal to
| |
− | |x >= y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |<=
| |
− | |<=
| |
− | |
| |
− | |
| |
− | |Less than or equal to
| |
− | |x <= y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | ! rowspan="3" |'''Logical Operators'''
| |
− | |and
| |
− | |&&
| |
− | |
| |
− | |
| |
− | |'''Logical and:''' Returns True if both statements are true
| |
− | |x < 5 and x < 10
| |
− | |x < 5 && x < 10
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |or
| |
− | |<nowiki>|| </nowiki>
| |
− | |
| |
− | |
| |
− | |'''Logical or:''' Returns True if one of the statements is true
| |
− | |x < 5 or x < 4
| |
− | |<nowiki>x < 5 || x < 4</nowiki>
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |not
| |
− | |!
| |
− | |
| |
− | |
| |
− | |'''Logical not:''' Reverse the result, returns False if the result is true
| |
− | |not(x < 5 and x < 10)
| |
− | |!(x < 5 && x < 10)
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | ! rowspan="2" |'''Identity Operators'''
| |
− | |is
| |
− | |
| |
− | |
| |
− | |
| |
− | |Returns true if both variables are the same object
| |
− | |x is y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |is not
| |
− | |
| |
− | |
| |
− | |
| |
− | |Returns true if both variables are not the same object
| |
− | |x is not y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | ! rowspan="2" |'''Membership Operators'''
| |
− | |in
| |
− | |
| |
− | |
| |
− | |
| |
− | |Returns True if a sequence with the specified value is present in the object
| |
− | |x in y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |not in
| |
− | |
| |
− | |
| |
− | |
| |
− | |Returns True if a sequence with the specified value is not present in the object
| |
− | |x not in y
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | ! rowspan="6" |'''Bitwise Operators'''
| |
− | |& <br />
| |
− | |
| |
− | |
| |
− | |
| |
− | |'''AND'''
| |
− | | |
− | Sets each bit to 1 if both bits are 1
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |<nowiki>|</nowiki>
| |
− | |
| |
− | |
| |
− | |
| |
− | |'''OR'''
| |
− | | |
− | Sets each bit to 1 if one of two bits is 1
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |^
| |
− | |
| |
− | |
| |
− | |
| |
− | |'''XOR'''
| |
− | | |
− | Sets each bit to 1 if only one of two bits is 1
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |~
| |
− | |
| |
− | |
| |
− | |
| |
− | |'''NOT'''
| |
− | | |
− | Inverts all the bits
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |<<
| |
− | |
| |
− | |
| |
− | |
| |
− | |'''Zero fill left shift'''
| |
− | | |
− | Shift left by pushing zeros in from the right and let the leftmost bits fall off
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | |>>
| |
− | |
| |
− | |
| |
− | |
| |
− | |'''Signed right shift'''
| |
− | | |
− | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |
| |
− | |-
| |
− | ! colspan="11" |Added by myself
| |
− | |-
| |
− | ! rowspan="3" |String concatenation
| |
− | | rowspan="3" |
| |
− | | rowspan="3" |
| |
− | | +
| |
− | | rowspan="3" |
| |
− | |
| |
− | |
| |
− | |
| |
− | |<syntaxhighlight lang="javascript">
| |
− | "Coucou ' + 'c\'est ' + 'nous !";
| |
− | </syntaxhighlight>
| |
− | |
| |
− | |
| |
− | |-
| |
− | |conca()
| |
− | |
| |
− | |
| |
− | |
| |
− | |<syntaxhighlight lang="javascript">
| |
− | 'Coucou '.concat('c\'est ', 'nous !');
| |
− | </syntaxhighlight>
| |
− | |
| |
− | |
| |
− | |-
| |
− | |join()
| |
− | |
| |
− | |
| |
− | |
| |
− | |<syntaxhighlight lang="javascript">
| |
− | ['Coucou ', 'c\'est ', 'nous !'].join();
| |
− | </syntaxhighlight>
| |
− | |
| |
− | |
| |
− | |}
| |
− | <section end="operators" />
| |
− | | |
− | | |
− | <br />
| |
− | | |
− | ==Some important Methods==
| |
− | | |
− | | |
− | <br />
| |
− | ===print===
| |