diff --git a/source/developer-guides/documentation/vala-for-java-devs.rst b/source/developer-guides/documentation/vala-for-java-devs.rst new file mode 100644 index 0000000..050fc20 --- /dev/null +++ b/source/developer-guides/documentation/vala-for-java-devs.rst @@ -0,0 +1,13 @@ +Vala for Java Developers +======================== +Vala is an object-oriented programming language with a syntax that is very similar to Java. As a Java developer, you will find many familiar concepts and constructs in Vala, making it easier to learn and transition to this language. + +Despite these similarities, there are also some differences between Vala and Java, such as Vala's use of reference counting for memory management instead of garbage collection.. + +Throughout this guide, we will explore the similarities and differences between Vala and Java, helping you leverage your Java knowledge to quickly become proficient in Vala programming. + +.. toctree:: + :glob: + :maxdepth: 1 + + vala-for-java-devs/* diff --git a/source/developer-guides/documentation/vala-for-java-devs/01-source-file-and-compilation.rst b/source/developer-guides/documentation/vala-for-java-devs/01-source-file-and-compilation.rst new file mode 100644 index 0000000..7521983 --- /dev/null +++ b/source/developer-guides/documentation/vala-for-java-devs/01-source-file-and-compilation.rst @@ -0,0 +1,81 @@ +Source File and Compilation +============================ + +Source Files +------------ + +Java: ``*.java`` + +Vala: ``*.vala`` + +Compilation +----------- + +Java: compiled to JVM byte code (``.class`` files) + +.. code-block:: bash + + $ javac SourceFile1.java SourceFile2.java + +Vala: compiled to native code via C code as intermediate code + +.. code-block:: bash + + $ valac source1.vala source2.vala -o program + +Vala's standard object system is GObject, compiled Vala libraries are valid C libraries. + +Using Libraries +--------------- + +Java: ``.jar`` files + +.. code-block:: bash + + $ javac -classpath foo-1.0.jar;bar-3.0.jar SourceFile.java + +Vala: packages (C libraries with ``.vapi`` files) + +.. code-block:: bash + + $ valac --pkg foo-1.0 --pkg bar-3.0 source.vala + +Java: + +.. code-block:: java + + import javax.swing.*; + + package org.foo.bar; + + // ... + +Vala: namespaces, not related to directory hierarchy, no reverse domain name scheme + +.. code-block:: vala + + using Gtk; + + namespace Foo.Bar { + // ... + } + +Vala namespaces may contain methods without classes. They are implicitly static. + +Default Import +-------------- + +Java: package ``java.lang.*`` imported by default + +Vala: namespace ``GLib`` imported by default + +Main Entry Point +---------------- + +Java: ``public static void main(String[] args)`` + +Vala: ``static int main (string[] args)`` + +May be outside a class, may be private, may return ``int`` (exit code), ``args`` argument is optional + +INSERT IMAGE HERE \ No newline at end of file diff --git a/source/developer-guides/documentation/vala-for-java-devs/02-simple-data-types.rst b/source/developer-guides/documentation/vala-for-java-devs/02-simple-data-types.rst new file mode 100644 index 0000000..196a13b --- /dev/null +++ b/source/developer-guides/documentation/vala-for-java-devs/02-simple-data-types.rst @@ -0,0 +1,47 @@ +Simple Data Types +================= + +Basic Types +----------- + +In Vala, the sizes of standard types (``int``, ``long``, etc.) are architecture-dependent. To get the size of a type in bytes, you can use the ``sizeof`` operator. For example: + +.. code-block:: vala + + int size = sizeof (int); + +Vala provides additional types with architecture-independent guaranteed sizes: + +- Signed: ``int8``, ``int16``, ``int32``, ``int64`` +- Unsigned: ``uint8``, ``uint16``, ``uint32``, ``uint64`` + +Note that there is no ``byte`` type in Vala. Instead, you can use ``uint8`` or ``int8``. + +Vala uses ``bool`` instead of ``boolean`` for boolean values. + +Vala also has an additional basic type called ``unichar``, which represents a Unicode character. + +Constant Modifier +----------------- + +In Vala, the ``const`` keyword is used to declare constants, similar to ``final`` in Java. + + +Methods on Basic Types +---------------------- + +Vala's basic types have methods that can be called directly on the values. For example: + +.. code-block:: vala + + int a = (-4).abs (); + string s = a.to_string (); + int b = int.max (5, 7); // static method call on 'int' + +In the above code: + +- ``(-4).abs ()`` calls the ``abs`` method on the integer value ``-4``, returning its absolute value. +- ``a.to_string ()`` converts the integer ``a`` to a string representation. +- ``int.max (5, 7)`` calls the static ``max`` method on the ``int`` type, returning the maximum value between ``5`` and ``7``. + +These are just a few examples of the methods available on Vala's basic types. diff --git a/source/developer-guides/documentation/vala-for-java-devs/03-strings.rst b/source/developer-guides/documentation/vala-for-java-devs/03-strings.rst new file mode 100644 index 0000000..2867914 --- /dev/null +++ b/source/developer-guides/documentation/vala-for-java-devs/03-strings.rst @@ -0,0 +1,34 @@ +Strings +======== + ++------------------+---------------------------+ +| Java | Vala | ++==================+===========================+ +| Data type: String| Data type: string (lower | +| | case) | ++------------------+---------------------------+ +| Equality test: | Equality test: | +| str1.equals(str2)| str1 == str2 | ++------------------+---------------------------+ + +String comparisons compare content, not reference. You can compare strings lexicographically with ``<``, ``>``, ``<=``, ``>=`` etc. Strings can be used with switch. + +Vala strings are UTF-8 encoded. + +Vala supports verbatim strings: ``"""..."""`` + +.. code-block:: vala + + string verbatim = """Verbatim strings don't evaluate escape sequences + like \n, \t, ... and may span multiple lines. + The line breaks are part of the string. + You can use quotation marks (") and backslashes (\) + inside a verbatim string without escaping them."""; + +Vala supports string templates: ``@"..."``. String templates may contain expressions, prefixed by a ``$`` sign. + +.. code-block:: vala + + string name = "John"; + stdout.printf (@"Welcome, $name!"); + stdout.printf (@"3 + 2 = $(3 + 2)"); \ No newline at end of file diff --git a/source/developer-guides/documentation/vala-for-java-devs/04-arrays.rst b/source/developer-guides/documentation/vala-for-java-devs/04-arrays.rst new file mode 100644 index 0000000..cbf311e --- /dev/null +++ b/source/developer-guides/documentation/vala-for-java-devs/04-arrays.rst @@ -0,0 +1,44 @@ +Arrays +====== + +Dynamic Growth +-------------- + +You can add elements to arrays dynamically by using the ``+=`` operator. The array will be reallocated with sizes of powers of two: + +.. code-block:: vala + + int[] squares = {}; + for (int i = 0; i < 100; i++) { + squares += i * i; + } + +No Boundary Checking +-------------------- + +However, there is no runtime boundary checking for arrays in Vala: + +.. code-block:: vala + + int[] a = new int[10]; + a[20] = 1; // not safe! + +(Optional boundary checking is planned for a future version of Vala.) + +Multi-Dimensional Arrays +------------------------ + +Java: jagged multi-dimensional arrays ``[][]`` (arrays of arrays) + +.. code-block:: java + + int[][] matrix = new int[3][]; + for (int[] row : matrix) { + row = new int[4]; + } + +Vala: rectangular multi-dimensional arrays ``[,]``, ``[,,]``, etc. (allocated as one contiguous memory block), jagged array support planned + +.. code-block:: vala + + int[,] matrix = new int[3,4]; diff --git a/source/developer-guides/documentation/vala-for-java-devs/05-naming-conventions.rst b/source/developer-guides/documentation/vala-for-java-devs/05-naming-conventions.rst new file mode 100644 index 0000000..eb29571 --- /dev/null +++ b/source/developer-guides/documentation/vala-for-java-devs/05-naming-conventions.rst @@ -0,0 +1,44 @@ +Naming Conventions +================== + +Java +---- + +* classes, interfaces, enums: ``CamelCase`` +* methods, local variables, fields: ``mixedCamelCase`` +* constants, enum values: ``UPPER_CASE`` + +Example: + +.. code-block:: java + + public class MyClass { + private static final int MAX_VALUE = 100; + private String myField; + + public void myMethod() { + int localVariable = 42; + } + } + +Vala +---- + +* classes, interfaces, structs, enums, delegate types, namespaces: ``CamelCase`` +* methods, local variables, fields, properties, signals: ``lower_case`` +* constants, enum values: ``UPPER_CASE`` + +Example: + +.. code-block:: vala + + public class MyClass { + private const int MAX_VALUE = 100; + private string my_field; + + public void my_method() { + int local_variable = 42; + } + } + +No non-ASCII letters for identifiers allowed. You can use Vala keywords as identifiers if you prefix them with ``@``. The at sign is not considered as part of the name. diff --git a/source/developer-guides/documentation/vala-for-java-devs/06-foreach.rst b/source/developer-guides/documentation/vala-for-java-devs/06-foreach.rst new file mode 100644 index 0000000..c0caf0b --- /dev/null +++ b/source/developer-guides/documentation/vala-for-java-devs/06-foreach.rst @@ -0,0 +1,16 @@ +Foreach +======= + +Java: + +.. code-block:: java + + for (int i : numbers) { + } + +Vala: + +.. code-block:: vala + + foreach (int i in numbers) { + } diff --git a/source/developer-guides/documentation/vala-for-java-devs/07-inheritance.rst b/source/developer-guides/documentation/vala-for-java-devs/07-inheritance.rst new file mode 100644 index 0000000..862c28a --- /dev/null +++ b/source/developer-guides/documentation/vala-for-java-devs/07-inheritance.rst @@ -0,0 +1,20 @@ +Inheritance +=========== + +Java: ``extends``, ``implements``:: + + public class Demo extends Foo implements Bar { + public Demo() { + super(); + } + } + +Vala: colon followed by comma separated list, both for super class and interfaces:: + + public class Demo : Foo, Bar { + public Demo () { + base (); + } + } + +``super`` is called ``base`` in Vala. diff --git a/source/developer-guides/documentation/vala-for-java-devs/08-methods.rst b/source/developer-guides/documentation/vala-for-java-devs/08-methods.rst new file mode 100644 index 0000000..a19aacf --- /dev/null +++ b/source/developer-guides/documentation/vala-for-java-devs/08-methods.rst @@ -0,0 +1,77 @@ +Methods +========= + +Method overloading +------------------- + +Java: + +.. code-block:: java + + public class Demo { + public void draw(String text) { } + public void draw(Shape shape) { } + + /* Method overloading + chaining for convenience methods with less arguments */ + void f(int x, String s, double z) { } + void f(int x, String s) { + f(x, s, 0.5); + } + void f(int x) { + f(x, "hello"); + } + } + +Vala: + +.. code-block:: vala + + public class Demo : Object { + public void draw_text (string text) { + } + public void draw_shape (Shape shape) { + } + + /* Method with argument default values */ + void f (int x, string s = "hello", double z = 0.5) { + } + } + +Vala does not support method overloading because libraries written in Vala are intended to be usable by C programmers as well with meaningful function names. + + +Method overriding +------------------- + +Java: + +.. code-block:: java + + public class Super { + public int myMethod(int x, int y) { } + public final void anotherMethod() { } + } + + public class Sub extends Super { + @Override + public int myMethod(int x, int y) { + super.myMethod(x, y); + // ... + } + } + +Vala: + +.. code-block:: vala + + public class Super : Object { + public virtual int my_method (int x, int y) { } + public void another_method () { } + } + + public class Sub : Super { + public override int my_method (int x, int y) { + base.my_method (x, y); + // ... + } + } diff --git a/source/developer-guides/documentation/vala-for-java-devs/09-type-inference.rst b/source/developer-guides/documentation/vala-for-java-devs/09-type-inference.rst new file mode 100644 index 0000000..7b4d38f --- /dev/null +++ b/source/developer-guides/documentation/vala-for-java-devs/09-type-inference.rst @@ -0,0 +1,105 @@ +Type Inference +============== + +Vala supports a mechanism called type inference (implicit typing) for local variables: Local variables may be declared using the ``var`` keyword instead of the type name if the compiler can deduce (infer) the type from the initial assignment. This helps avoiding unnecessary redundancy and is especially useful for generic types. Examples: + +.. code-block:: vala + + var obj = new Object (); + var map = new HashMap (); + var str = "hello, world"; + var arr = new int[10]; + +instead of: + +.. code-block:: vala + + Object obj = new Object (); + HashMap map = new HashMap (); + string str = "hello, world"; + int[] arr = new int[10]; + +Still, everything is statically typed. + + + +Object Base Class +----------------- + +Java: implicit inheritance from ``Object`` (``java.lang.Object``): + +.. code-block:: java + + public class Foo { + // ... + } + +Vala: no implicit inheritance from ``Object`` (``GLib.Object``): + +.. code-block:: vala + + public class Foo : Object { + // ... + } + +What happens if you don't inherit from ``Object``? Nothing terrible. These classes will be slightly more lightweight, however, they will lack some features such as property change notifications, and your objects won't have a common base class. Usually inheriting from ``Object`` is what you want. + +Multiple Constructors +--------------------- + +Java: constructor overloading: + +.. code-block:: java + + public class Foo { + public Foo() { } + public Foo(int foo) { } + public Foo(String bar) { } + } + + new Foo(); + new Foo(42); + new Foo("hello"); + +Vala: named constructors instead of constructor overloading: + +.. code-block:: vala + + public class Foo : Object { + public Foo () { } + public Foo.with_foo (int foo) { } + public Foo.from_bar (string bar) { } + } + + new Foo (); + new Foo.with_foo (42); + new Foo.from_bar ("hello"); + +Constructor Chaining +-------------------- + +Java: this(): + +.. code-block:: java + + class Foo { + public Foo() { + this("bar"); + } + + public Foo(string bar) { + } + } + +Vala: this() or this.name_addition(): + +.. code-block:: vala + + class Foo : Object { + public Foo () { + this.with_bar ("bar"); + } + + public Foo.with_bar (string bar) { + } + } diff --git a/source/developer-guides/documentation/vala-for-java-devs/10-code-organization.rst b/source/developer-guides/documentation/vala-for-java-devs/10-code-organization.rst new file mode 100644 index 0000000..33c7fac --- /dev/null +++ b/source/developer-guides/documentation/vala-for-java-devs/10-code-organization.rst @@ -0,0 +1,51 @@ +Code Organization +================= + +Files +----- + +Java: + - one toplevel class per file + - file name resembles class name + +Vala: + - a Vala source file may contain multiple classes + - file name doesn't need to resemble a class name + +Hierarchy +--------- + +Java: + - packages, represented by directory hierarchy + - reverse domain name scheme + +.. code-block:: java + + import javax.swing.*; + + package org.foo.bar; + // ... + +Vala: + - namespaces, not related to directory hierarchy + - no reverse domain name scheme + +.. code-block:: vala + + using Gtk; + + namespace Foo.Bar { + // ... + } + +Vala namespaces may contain methods without classes. They are implicitly static. + +Default Import +-------------- + +Java: + - package ``java.lang.*`` imported by default + +Vala: + - namespace ``GLib`` imported by default + diff --git a/source/index.rst b/source/index.rst index 1a79a1e..2c90465 100644 --- a/source/index.rst +++ b/source/index.rst @@ -30,7 +30,7 @@ Sections - `Tooling `_ - `Contributor Guide `_ - `Developer Guides `_ -- `FAQ `_ +- `FAQ `_ External Resources ------------------